.htaccess extentionless url rewrites breaking folder redirects?
Hi, on my sites, I'm wanting extentions to be removed, and then any extentioned requests get rewritten, also removing the trailing slash.
By doing this, people can land on the page they want whether they put html, php, htm, or shtml (if there's more common extentions for web pages will be good to know) or no extention at all.
Trouble is, folder/file redirects work finebefore I place in the htaccess rules I have put together, but after, nothing I do works.
Could anyone work out what is the culprit and what I can do instead to have the same effect but with working redirects?
I asked this on my hosting provider's forums InstaFree.com, and one suggested it could be the ordering is wrong? He suggested to put redirects above these rules. Trouble is though, Cpanel redirect tool places redirect entries on the bottom, so that'd not be a solution.
Also asked this on two other forums but just having no response. Apologies if this is in the wrong section but does seem the most appropriate
Here's the code. Thanks, Jase Wolf
# Rewrite urls to remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]
# Redirect http to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rewrite files to extensionless urls
RewriteRule ^([^/.]+)$ /$1.php [L]
RewriteRule ^([^/.]+)$ /$1.html [L]
RewriteRule ^([^/.]+)$ /$1.shtml [L]
RewriteRule ^([^/.]+)$ /$1.htm [L]
# Redirect external requests to extensionless urls
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ $1 [R=301,L]
RewriteCond %{THE_REQUEST} ^(.+)\.htm([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.htm$ $1 [R=301,L]
RewriteCond %{THE_REQUEST} ^(.+)\.shtml([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.shtml$ $1 [R=301,L]-
[COLOR=rgb(3, 42, 70)]Right after some testing this is the culprit, had a bit of a feeling it was this as I was also having redirect issues just with the php rewriting I were using. # Rewrite files to extensionless urls RewriteRule ^([^/.]+)$ /$1.php [L] RewriteRule ^([^/.]+)$ /$1.html [L] RewriteRule ^([^/.]+)$ /$1.shtml [L] RewriteRule ^([^/.]+)$ /$1.htm [L]
Using this code instead fixes it# Rewrite files to extensionless urls RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [NC,L] RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.*)$ $1.html[NC,L] RewriteCond %{REQUEST_FILENAME}.htm -f RewriteRule ^(.*)$ $1.htm [NC,L] RewriteCond %{REQUEST_FILENAME}.shtml -f RewriteRule ^(.*)$ $1.shtml [NC,L]
So WOOOHOOOOOOOOOOOOOOOO, four forums I posted this in, been trying to work this out for ages and finally work it out lol0 -
Hello, I'm happy to see you were able to address the issue. Thank you for updating us with the outcome. 0
Please sign in to leave a comment.
Comments
2 comments