.htaccess extentionless url rewrites breaking folder redirects?

Jase Wolf

New Member
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 fine before 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.

Here's the code. Thanks, Jase Wolf

Code:
# 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]
 
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.

Code:
# 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

Code:
# 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, been trying to work this out for ages, and despite posting on four places, I work it out :D How fantastic lol
 
Back
Top