I also want to 301 redirect any url that ends in .php. Then I internally redirect anything that ends in a / to the name before / plus .php. These both work fine when they are alone. But putting them in the same .htaccess I get a The page isn't redirecting properly.
What am I doing wrong here?
RewriteRule ^(.*)\.php$ /$1/ [R=301,L]
RewriteRule ^(.*)/$ $1.php [QSA,L]
These rules should work for you:
RewriteEngine On
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L,NE]
# To internally forward /dir/file/ to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Related
So I have recently uploaded my new website to hostgator.
I am trying to remove .php extension and add a trailing slash to all my urls.
For example
website.com/about.php should change to website.com/about/
I have the htaccess code that has worked for other websites on different hosts, but when i tried it for this website on hostgator it doesnt work.
This is the code I tried:
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.co.uk/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www.website.co.uk [NC]
RewriteRule ^(.*)$ http://website.co.uk/$1 [L,R=301]
Currently I now have this in my htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
# remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Hostgator added this htaccess for me which basically redirects http requests to https and removes http://www.website.co.uk and www.website.co.uk to website.co.uk
What can I do to remove .php extension and force trailing slash?
You can replace all the code in your site root .htaccess with this:
RewriteEngine On
# remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1/ [R=301,L,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301,NE]
# To internally rewrite /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
Make sure to test this in a new browser or after clearing your browser cache.
For starters, depending on your hosting environment you may need to add the following to the beginning of your htaccess file:
Options -MultiViews
If MultiViews is on the server will do implicit filename matching. So if you request page/ it could interpret that as page.php. Use the option above to disable MultiViews and see if that helps.
OK I've tried multiple solutions but still can't achieve it. so what I need to do is
mydomain.com/search?q=product&l=london
to
mydomain.com/search/product/london
Basically I'm using rewrite rules to hide .php extension by following this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So now, search.php converts into search and when user searches. it redirects to trailing slash instead of query string
Here what I've used so far
RewriteCond %{THE_REQUEST} /search/?q=([^&]+)&l=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [NC,L,R]
#skip the rule if the request is for dir
RewriteCond %{REQUEST_FILENAME} !-d
#skip the rule if the request is for file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite any other request to "/packagemenu.php?"
RewriteRule ^([^/]+)/([^/]+)/?$ /search?q=$1&l=$2 [NC,L]
AND
RewriteBase /search/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+) [NC]
RewriteRule ^ search/%1/%2/? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from pretty URL to actual one
RewriteRule ^search/([^/]+)/?$ search?q=$1 [NC,L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/?$ search?q=$1&l=$2 [NC,L,QSA]
But both of them doesn't work. I don't have so much knowledge about so please help me out.
Have your site root .htaccess as this:
Options -MultiViews
RewriteEngine On
RewriteBase /jobportal/
# To externally redirect /search.php?q=product&l=london to /search/query/london/
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/%2/? [R=301,L,NE]
# To externally redirect /search.php?q=product to /search/query/
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/? [R=301,L,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule ^ %1 [R=301,NE,L]
# ignore all rules below for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from /search/product/ to search.php?q=product
RewriteRule ^search/([^/]+)/?$ search.php?q=$1 [NC,L,QSA]
# internal forward from /search/product/london/ to search.php?q=product&l=london
RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&l=$2 [NC,L,QSA]
# internal forward from /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You can achieve the URL mydomain.com/search/product/london by using the following rule in your .htaccess.
RewriteEngine On
RewriteRule ^search/([^/]*)/([^/]*)$ /search?q=$1&l=$2 [L]
Just make sure you clear your cache before testing this.
Try as below,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/search$
RewriteCond %{QUERY_STRING} ^q=([^\/]+)&l=([^\/]+)$
RewriteRule ^ search/%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^search/([^\/]+)/([^\/]+)$ search.php?q=$1&l=$2 [QSA,L]
I've read many articles and cannot seem to get ALL of the combined .htaccess Rewrites to work together. I either get re-direct loops or one or a few do not work at all.
To be clear I'm looking for the following 5 to occur if needed:
Ensure www on all URLs
Ensure HTTPS for all version of site
Remove index.php from url
Remove all .php extension / Re-direct to url without .php extension
Could be in tandem with the previous: add trailing slash
Some examples:
example.com/index.php => https://www.example.com/
www.example.com/info/really-good-info.php => https://www.example.com/info/really-good-info/
www.example.com/about-us.php => https://www.example.com/about-us/
www.example.com/careers/index.php => https://www.example.com/careers/
Here is current .htaccess setup:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://www.example.com/$1/ [L,R=301]
</IfModule>
The above .htaccess is ONLY in my root folder and currently does 3 out of the 5 needed: changes to HTTPS, adds www and removes index.php. It does not remove any other .php files extension nor does it add trailing slash.
I see 2 issues:
Redirect rules appearing after rewrite ones
Adding .php should only happen after you ensure corresponding .php file exists.
Have it this way:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=302]
# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC]
RewriteRule ^ %1/ [R=302,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# Ensure all URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302]
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ index.php?$1 [L,QSA]
Make sure to clear your browser cache before testing this.
Try this to avoid the loop:
#non-www. http to www. https
RewriteCond %{ENV:HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
#non-www. https to www. https
RewriteCond %{ENV:HTTPS} on
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC]
RewriteRule ^ %1/ [R=302,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# Ensure all URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302]
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ index.php?$1 [L,QSA]
I'm having a problem in that after I remove the .php extension from my URL using a .htaccess re-write rule, I can no longer travel to directories 1 level down.
for example. if I navigate to www.gameandshame.com/register.php it will re-direct to www.gameandshame.com/register however, when I use a link to logout
www.gameandshame.com/auth/login.php it re-writes to www.gameandshame.com/auth/login however it produces a 404 error, the login and logout files are located inside the auth folder and it's pretty important I keep my website files ordered properly so I don't want to pull them to my main directory.
Does anybody know why they might be producing a 404 error?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^gameandshame.com [NC]
RewriteRule ^(.*)$ http://www.gameandshame.com/$1 [L,R=301]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.gameandshame.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.gameandshame.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
#Handle Profile Re-directs
RewriteRule ^profile/?$ profile.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/(.*)/?$ profile.php?u=$1 [NC,L]
#Handle Post re-directs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/?$ post.php [NC,L]
RewriteRule ^post/(.*)/?$ post.php?postid=$1 [NC,L]
Have these rules in root .htaccess:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^gameandshame\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.gameandshame.com/$1 [R=301,L,NE]
## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/(.+)/?$ profile.php?u=$1 [NC,L,QSA]
#Handle Post re-directs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/(.+)/?$ post.php?postid=$1 [NC,L,QSA]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
I want to redirect from extension URL to extensionless one. For example, /contact should read the contents of /contact.php. I use the following rules to achieve this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
There is a file called test.php that has a variable called "id". I want to redirect read requests from /test/1 to test.php?id=1
I use the following to achieve this:
RewriteRule ^test/([0-9]+)/?$ /test?id=$1
For some reason calling the test/1 file leads to a 500 internal server error. If I remove the rules to hide the php extension, it works again
Also how can I force a permanent redirect on the URL with extension? For example if someone ties to reach /contact.php should be redirected to /contact
Thanks
You can use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# your existing rule (fixed)
RewriteRule ^test/([0-9]+)/?$ /test.php?id=$1 [L,NC,QSA]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L]
This is in my htaccess ... Be sure to have mod_rewrite turned on in Apache...
RewriteEngine On
# Redirect .php requests to url without an extension
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
RewriteRule ^([^/.]+)$ $1.php [L]
# This will remove the trailing slash unless it's a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]