My .htaccess file is creating a redirect loop. I have searched online and tried lots of varieties including many from this site but still can't get it to work. The purpose of it is to remove the .html from the end of the URL.
Here is my code:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
Replace all of your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.+?)\.html?[/?\s] [NC]
RewriteRule ^ %1? [NE,R=301,L]
# To internally forward /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
## append trailing slash if needed
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
Related
I'm struggling to solve a redirection but without any success.
I changed the URLs of my site forcing a default language, before it was site.com/help/ and now it's site.com/en/help/. Thanks to help from Stack Overflow I made the redirection, but then I faced a new problem with the AMP pages: site.com/amp/help/ are now redirected to site.com/en/amp/help/ while they are supposed to be site.com/amp/en/help/.
Again, thanks to help on this site, I changed the structure of URLs to site.com/en/help/amp/ (amp always at the end). To achieve this, I had to delete the .php extension I had in some pages and also decided to remove the trailing slash.
I'm now facing two new issues: the 301 redirection to a non .php page and URLs with trailing slashes to a non trailing slash don't work. Below is my htaccess code.
RewriteEngine on
# amp
RewriteRule ^(.*/)?amp/(.+?)/?$ /$1$2/amp [R=301,NC,L]
## redirect to default language (fr)
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} !/inc
RewriteCond %{REQUEST_URI} !/ajax/
RewriteCond %{REQUEST_URI} !/img/
RewriteRule ^(?![a-z]{2}(?:[/-]|$))(.*)$ /fr/$1 [R=301,L,NE]
## Unless directory, remove trailing slash
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ /$1 [R=301,NE,L]
## add trailing slash in front of directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^[a-z]{2}(?:-[a-z]{2})?/(.+)$ /$1/ [L]
# remove .php
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !global.js
RewriteCond %{REQUEST_URI} !/ajax/
RewriteCond %{REQUEST_URI} !results.php
RewriteRule ^(.+)\.php(.*)$ /$1$2 [R=301,NC,NE,L]
## amp pages
RewriteRule ^(.*)/amp$ /$1?amp=1 [NC,QSA,L]
## folders of languages
#RewriteRule ^([a-z]{2}(?:-[a-z]{2})?)/(.*)$ /$2?lang=$1 [QSA,L]
RewriteRule ^([a-z]{2}|[a-z]{2}-[a-z]{2})$ /$2?lang=$1 [QSA,L]
RewriteRule ^([a-z]{2}|[a-z]{2}-[a-z]{2})/(.*)$ /$2?lang=$1 [QSA,L]
## hide .php extention
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !global.js
RewriteRule ^(.+?)/?$ $1.php [L]
Have it this way:
RewriteEngine on
# changed amp URLs
RewriteRule ^(.*/)?amp/(.+?)/?$ /$1$2/amp/ [R=301,NC,L]
## redirect to default language (en)
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} !/img/
RewriteRule ^(?![a-z]{2}(?:[/-]|$))(.*)$ /en/$1 [R=301,L,NE]
## Unless directory, remove trailing slash
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/amp/$ [NC]
RewriteRule ^(.+)/$ /$1 [R=301,NE,L]
## add trailing slash in front of directories after lang rewrite
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^[a-z]{2}(?:-[a-z]{2})?/(.+[^/])$ /$0/ [L]
# remove .php
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.+)\.php$ /$1/ [R=301,NC,NE,L]
## amp pages
RewriteRule ^(.+/)amp/?$ /$1?amp=1 [NC,QSA,L]
## folders of languages
RewriteRule ^([a-z]{2}(?:-[a-z]{2})?)/(.*)$ /$2?lang=$1 [QSA,L]
## hide .php extention
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !global.js
RewriteRule ^(.+?)/?$ $1.php [L]
Explanation of this trailing slash rule:
## add trailing slash in front of directories after lang rewrite
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^[a-z]{2}(?:-[a-z]{2})?/(.+[^/])$ /$0/ [L]
Take an example URI: /fr/cart.
In a later rule we remove lang component from URL and pass it as lang=<fr|en> query parameter. Part after lang parameter e.g. /cart doesn't have a trailing slash and if it is a real directory then /cart?lang=fr will be redirected to /cart/?lang=fr by Apache's mod_dir module and your internal URL will be exposed in browser.
So in this current rule we capture part after lang component and check if we don't have a trailing slash and it is a directory then this rule internally rewrites to /fr/cart/ with a trailing slash. Later rule then rewrites it to /cart/?lang=fr and mod_dir doesn't redirect anymore.
#anubhava's solution works perfectly well except for one little case: /fr/amp/page.php redirects to /fr/page.php/amp, I had to make some changed to the code and managed to make it work. Below is the updated code with small changes I made:
1- removed some slashes at the end of some rules as I don't need them any more
2- removed this rule RewriteCond %{REQUEST_URI} !/amp/$ [NC]
3- to fix the .php problem, I replaced RewriteRule ^(.+)\.php$ /$1/ [R=301,NC,NE,L] by RewriteRule ^(.+)\.php(.*)$ /$1$2 [R=301,NC,NE,L].
RewriteEngine on
## changed amp URLs
RewriteRule ^(.*/)?amp/(.+?)/?$ /$1$2/amp [R=301,NC,L]
## redirect to default language (en)
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} !/img/
RewriteRule ^(?![a-z]{2}(?:[/-]|$))(.*)$ /en/$1 [R=301,L,NE]
## Unless directory, remove trailing slash
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ /$1 [R=301,NE,L]
## add trailing slash in front of directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^[a-z]{2}(?:-[a-z]{2})?/(.+)$ /$1/ [L]
# remove .php
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.+)\.php(.*)$ /$1$2 [R=301,NC,NE,L]
## amp pages
RewriteRule ^(.+/)amp$ /$1?amp=1 [NC,QSA,L]
## folders of languages
RewriteRule ^([a-z]{2}(?:-[a-z]{2})?)/(.*)$ /$2?lang=$1 [QSA,L]
## hide .php extention
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !global.js
RewriteRule ^(.+?)/?$ $1.php [L]
I want directory www.example.com/core to be translated into string, instead of just dissalowing access to it. is that possible?
UPDATE (STILL NO LUCK):
My current .htaccess
#Options -Multiviews
RewriteEngine On
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Front Controller...
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ launcher.php?urls=$1 [QSA,L]
RewriteRule . launcher.php [L]
Here are some scenarios:
I write a url www.example.com/some-dir-that-does-not-exist and it works fine.
I write a url www.example.com/url-that-DOES-exist and the browser redirects it to www.example.com/url-that-DOES-exist/?url=url-that-DOES-exist
That is due to mod_dir module adding a trailing slash in front of real directories and making a 301 redirect after your rewrite rule.
To fix have it like this:
DirectorySlash Off
RewriteEngine On
RewriteBase /public/
RewriteCond %{THE_REQUEST} /launcher\.php [NC]
RewriteRule ^ - [F]
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ launcher.php?urls=$1 [QSA,L]
Your code will become:
#Options -Multiviews
RewriteEngine On
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /public
# Treat existing /core directory as non-existing (handled by launcher.php)
RewriteCond %{REQUEST_URI} ^/core(/.*)?$
RewriteRule . launcher.php [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ launcher.php?urls=$1 [QSA,L]
RewriteRule . launcher.php [L]
I'm trying to rewrite the url with .htaccess file but it throws an error in some situations.
I've been trying to convert
example.com/room.php to example.com/room and it works, but also, to convert
example.com/room.php?room=5 to example.com/room/5
RewriteEngine On
RewriteBase /
# Remove the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Redirect page.php to page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
# Internally redirect page to page.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteRule ^room/(.*?)$ /room.php?room=$1 [L,NC]
And it successfully converts room.php to room, buy when I try to visit room/5 it gives me 500 Internal Server Error. I've tried everything but still don't know what the problem is.
Have it like this:
Options -MultiViews
RewriteEngine On
RewriteBase /
# Remove the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Redirect page.php to page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
RewriteRule ^room/([^/]+)/?$ room.php?room=$1 [L,NC,QSA]
# Internally redirect page to page.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
The following regex works perfectly except for under the condition when the user goes to examplesite1.com/site1. When this happens the page fails because of multiple redirects in which it continues to add the request=site1 query to the URL. I thought that adding RewriteCond %{ENV:REDIRECT_STATUS} ^$ would fix this issue however that seemed to not be the case.
This setup is for a multi-domain website. There are three folders site1, site2, and shared. There are also rewrite rules for site2 and shared that are not shown as they do not effect this issue.
# if the resource exists in site1 return it to the user
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
# no file found, send URI to CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
complete .htaccess file:
AddDefaultCharset UTF-8
# Disallow browsing file directories
Options -Indexes
RewriteEngine on
RewriteBase /
#remove the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
RewriteCond %{HTTP_HOST} examplesite2.com$
RewriteCond %{DOCUMENT_ROOT}/site2/%{REQUEST_URI} -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /site2/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/shared/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /shared/$1 [QSA,L]
# resources not to be public are sent to CMS
RewriteCond %{REQUEST_FILENAME} (\.inc\.|\.tpl$)
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
# no resource found, send URI to CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
Try adding an extra condition:
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{REQUEST_URI} !^/site1/
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
What looks like may be happening here is that you need to prevent a redirect from happening after one of your other rules get applied. Try adding this right below RewriteBase /:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
I'm trying to make it so requests to http://www.domain.tld/folder/filename.php get 301 redirected to http://www.domain.tld/folder/filename (ie. no php) and am having some difficulty doing so.
Here's my .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule (.*) $1.php [L]
RewriteRule (.*)\.php $1 [L,R=301]
First RewriteRule works as I'd expect. Second... not so much.
As written requests to http://www.domain.tld/folder/filename.php get 301 redirected to http://www.domain.tld/home/username/public_html/folder/filename
If I change that last RewriteRule to do /$1 instead of $1 I get directed to http://www.domain.tld/filename and not http://www.domain.tld/folder/filename.
Any ideas?
Thanks!
Keep your .htaccess like this for hiding .php extension:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]