I am looking for a Mod Rewrite script that will do the following;
change my static files with HTML extension to just the name of the file
e.g www.domain.com/contact.html to www.domain.com/contact
Thanks
P
I am looking for a Mod Rewrite script that will do the following;
change my static files with HTML extension to just the name of the file
e.g www.domain.com/contact.html to www.domain.com/contact
Thanks
P
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /$1.html?&%{QUERY_STRING} [NC,L]
should work
Thanks I will try that
In essence it looks okay, but it won’t work because it will send Apache in an infinite loop, because it will append the .html once, then start over, append .html, start over, append .html, etc.
What you need to do is ensure that the requested URL doesn’t end in .html already.
So:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\\.html$
RewriteRule ^([a-zA-Z0-9-\\/]+)$ $1.html [L]
Note that I’ve removed the IndexIgnore, since that has no business being in the .htaccess – it should be in the server config. Okay, you could add it to the .htaccess but you really shouldn’t have to.
I’ve also removed the RewriteBase / because that is to undo any effects of a Redirect or a RedirectMatch, and there is none in the .htaccess so you don’t need it *
Lastly, try to steer clear from (.*) if at all possible; it generally causes more problems than it solves. I’ve replaced it with ([a-zA-Z0-9\/-]+) to catch only characters, digits, slashes and dashes.
Lastly, the query string is copied to the new URL automatically; you don’t need to add %{QUERY_STRING} to the end of the URL to make Apache do that.
Thanks a lot guys for helping out
I want to put an exception for php files, so any file with .php, it should bypass it
.php already bypass the code I gave in post #4. What is the problem you’re having exactly? Could you provide a bit more detail?
OK,I have a site with static files ending with .HTML
I also have a folder called blog with this link www.website/blog
Now the code you sent is adding .html to this link www.website/blog
and thereby causing error, as blog.html doesn’t exist.
The other static files work fine
Ah yes I get it now. You need to make sure the requested file is not an actual directory.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\\.html$
RewriteRule ^([a-zA-Z0-9-\\/]+)$ $1.html [L]
Thanks a lot, it work