I have my HTML files called "thing.html" and script files called "script.pl", but I don't want my site visitors to know the difference. If they ask for "http://server.com/script" I want the URL to be properly rewritten to go to the "script.pl" file, BUT, if they type "/script.pl", I'd like them to be redirected to "script", which is then rewritten to have the ".pl" on the end.
Essentially, everything the user asks for ending in ".html" or ".pl" should get redirected to a page without it, and everything without the extension should get rewritten properly so that the webserver finds the file.
The point of this is that a user might ask for
http://server.com/thing_which_used_to_be/a_static_page.html
they should be redirected to
and served up with a CGI script.http://server.com/thing_which_used_to_be/a_static_page
The following works alright,
- Code: Select all
RewriteCond %{THE_REQUEST} \.html[[:space:]]
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteCond %{THE_REQUEST} \.pl[[:space:]]
RewriteRule ^(.*)\.pl$ $1 [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1.pl -f
RewriteRule ^(.*)$ $1.pl [L]
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*)$ $1.html [L]
but not for querystring requests. ("http://server.com/script.pl?arg=val" gets served up as-is, without rewriting.)
(Try it in a top-level .htaccess file: request "/index.html". It should get redirected to without the extension, but the file served up anyway.)
Yes, I know I'm a pedant, and, yes, I know that I could theoretically solve the problem by redirecting everything to some 'hidden' directory which actually contains the files... or redirecting to a CGI script which sorts everything out, but I'd quite like to do this using mod_rewrite, if possible.
It's not easy. The obvious approaches seem doomed to fail. (If I remove the condition which examines %{THE_REQUEST}, the whole thing goes into an infinite redirect loop. Which is not good.)
Any 'mod_rewrite' masters out there who can tell me either how to do it, or that it cannot be done?
Thanks,
--Andrew