Redirecting, then rewriting back again... Possible? Or not?

Discuss practical ways rearrange URLs using mod_rewrite.

Redirecting, then rewriting back again... Possible? Or not?

Postby andrewf » Tue Jan 21, 2003 2:16 pm

Hello,

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
http://server.com/thing_which_used_to_be/a_static_page
and served up with a CGI script.

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
andrewf
 
Posts: 2
Joined: Tue Jan 21, 2003 1:51 pm

Postby Caterham » Tue Feb 08, 2005 1:44 pm

may be you aren't interested in a reply any more, but may be some one else has nearly the same problem :D:

just remove the
[[:space:]]
fom the Conditions

.htaccess document root wrote:
Code: Select all
RewriteCond   %{THE_REQUEST}   \.html
RewriteRule   ^(.*)\.html$   /$1   [R=301,L]

RewriteCond   %{THE_REQUEST}   \.pl
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]
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 43 guests

cron