How to make mod_rewrite silently redirect and pass parameter to index when the {REQUEST_FILENAME} is an existing directory or file - file

Synopsis:
localhost/root/admin is an existing folder
I want /root/admin to go to /root/index.php?url=admin without changing the url in the address bar.
The only except is if the requested file is an image
This is what I have written for the .htaccess file in localhost/root to try and do all this:
RewriteEngine On
RewriteBase /root/
RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !(.*\.(png|jpg|gif))$ [NC]
RewriteRule (.*) index.php?url=$1 [QSA,L]
What this does instead is redirect localhost/root/admin to localhost/root/index.php and changes the url in the address bar to localhost/root/admin/?url=admin.
It will only not do this if I add a trailing slash (/) onto "admin".
I know that, on my mac laptop, this mod_rewrite code does exactly what it is meant to, but only when running it in the "Sites" folder and not in the localhost htdocs folder.
Any thoughts on what I can do to fix this?

I think you're just missing the "PT" flag to prevent an actual redirect. PT (Passthrough) means that Apache redirects the resource served internally, but the browser sees the same URL. (See the Apache wiki for more details: http://wiki.apache.org/httpd/RewriteFlags/PT)
RewriteEngine On
RewriteBase /root/
RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !(.*\.(png|jpg|gif))$ [NC]
RewriteRule (.*) index.php?url=$1 [PT,QSA,L]
Note the only change was to add the flag. I tested this on my server and saw the behavior that I think you want.

Related

.htaccess 500 internal server error when routing api

I'm currently running php on an apache server locally, with a React frontend.
This is how my current .htaccess is laid out:
Options -MultiViews
RewriteEngine On
RewriteRule ^api/(.*)$ api/$1\.php [L]
RewriteCond %{REQUEST_URI} !^/api.*?
RewriteRule ^ index.html [QSA,L]
The bottom condition is so that routing works in my React app. I'm then taking the production build and copying it into my htdocs.
The routing works, however, I want to be able to call the .php files inside my /api directory without using the file extension. So I want anything that comes after /api/ to be redirected to whatever is entered, followed by .php.
E.g. /api/authentication would go to /api/authentication.php, and /api/register would go to /api/register.php, and so on.
With this current setup, I'm getting a 500 internal server error when making requests to /api/authentication etc.
Is there something wrong with my .htaccess file?
Your first rule is looping as you're matching .*. You may use:
Options -MultiViews
RewriteEngine On
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteRule ^api/(.+)$ api/$1.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^api index.html [L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC] will skip rewriting when a URI ends with .php.

My css file is requiring /index.php as part of the url

Ive taken a copy of expressionengine from our production server to a dev server and its installed and running. I can access the admin console fine and Ive updated my paths.
The problem is that when I try and view my site the main page loads but no CSS is downloaded. The error Im getting is
The requested URL /presentation/layout was not found on this server.
I cant access the file from http://localhost/presentation/layout but I can access from
http://localhost/index.php/presentation/layout
Ive updated my .htaccess file from the following guide
https://docs.expressionengine.com/latest/urls/remove_index.php.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
I can access the site now from http://localhost without the index.php but its still looking for it for the CSS file. Any idea how I can resolve this?
First thing: don't put your css file in a template unless you absolutely have to.
You only need dynamical css files if the styles are depending on the data in a channel like
{exp:channel:entries}
.{title} {
color: #{color_custom_field};
}
{/exp:channel:entries}
If you put it in a template it will be processed by the template parser on every page view. This will add extra queries/processing which isn't needed for a static file.
What version of EE are you on?
All of us EE guys are over at https://expressionengine.stackexchange.com/ by the way

.htaccess - Redirect everything to homepage except one directory

I'm creating an AngularJS app and want to avoid the # in the URL. I learned that I needed to add the following .htaccess rules to make it work:
RewriteEngine On
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]
That worked great. However, now my calls to my web services are not working. The web service files are inside an /api/ directory, e.g. http://example.com/api/. My guess is that when the api calls try to access those files, they also get redirected and break. So how can I modify the rules above to redirect everything to the homepage, except links that are going to any file inside the api folder? I tried to find an existing answer to this, but while there were many similar ones, none of them was exactly what I needed. Thanks!
I found the code I needed:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /
That's all you need. Got it from: https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/

Cake site not finding images nor CSS files after moving it to my localhost?

I'm using cake1.3.5
I recently moved a live working site into my localhost:
I set config.php to my local database but when I enter the site, I see this:
Is it there any additional step I am missing (concerning the file path or something)?
Cake directs all its call to webroot folder (by default called URL rewriting). This is achieved via .htaccess file in the document root, which MUST have (for URL rewriting to work)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
The .htaccess file inside the webroot folder should contain
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d // this line traps requests for directory
RewriteCond %{REQUEST_FILENAME} !-f // this line traps requests for file names
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
The two lines RewriteCond %{REQUEST_FILENAME} !-d & RewriteCond %{REQUEST_FILENAME} !-f are essential in parsing requests for directories and file names.
The line RewriteRule ^(.*)$ index.php?/$1 [QSA,L] deals with anything which is not a file or directory.
Check if mod_rewrite is enabled or not.
There must be two .htaccess file, one in the root folder and other one in the webroot folder. The contents of both these .htaccess files must be different.
You may wanna check out URL rewriting
You can turn off URL rewriting from Core Configuration
having gone through the same scenario, it turns out that the localhost did not recognise short style tags are only available when they are enabled in php.ini configuration file on servers.
When I changed the tags to all the pages appeared normally.

htaccess various rewriting needs

I have a website let's say http://myweb.com and I want to migrate it into a subfolder of the root directory http://myweb.com/subfolder/
I need the following things, I guess using the htaccess file:
1) Redirect all non-www to www
2) Redirect all files in the root to equivalent files in the subfolder at the same time changing all htm extension to html, that is a file in the root file.htm must be rewritten to a file in the subfolder like subfolder/file.html
3)I want that in the address is still displayed www.myweb.com rather than www.myweb.com/subfolder
is it all possible? And how? Thanks for any answer
Add these rules to the htaccess in your document root:
RewriteEngine On
# 1) Redirect all non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
# 2) Redirect all files in the root to equivalent files in the subfolder
# at the same time changing all htm extension to html
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(.*)\.htm$ /subfolder/$1.html [L]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(.*)$ /subfolder/$1 [L]
The last rules don't have the R flag so they won't cause an external redirect (thus changing what's in the browser's URL address bar).

Resources