Forum Moderators: bakedjake

Message Too Old, No Replies

mod_rewrite: redirect all URIs containing .cfm and/or a query string

mod_rewrite: redirect all URIs containing .cfm and/or a query string

         

forefront

6:30 pm on Aug 26, 2003 (gmt 0)

10+ Year Member



I am trying to create a mod_rewrite rule for so that if the URI requested contains either a question mark and/or there is the filename extension .cfm anywhere in the URI, then the request is sent onto the script redirect.php - all other requested URIs are left alone.

Also, I want the rule to take effect whether the URI is "short" i.e. foo.com/page.cfm?var=23 or "long" foo.com/1/2/3/4/5/page.cfm?var=23 or foo.com/1/2/3/4/5/6/page.cfm without having to specify all the levels of directory structure within the rule.

I have tried a couple of rules but just get server errors.

jdMorgan

6:41 pm on Aug 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



forefront,

Welcome to WebmasterWorld [webmasterworld.com]!

Post what you've got, and let's see what's wrong with it... We generally try to help you learn, not do it for you.

Ref: Introduction to mod_rewrite [webmasterworld.com]
(See the cited regular expressions tutorial, and check out "anchors".)

Jim

claus

7:02 pm on Aug 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim's right, but there might be an issue here:

>> either a question mark and/or (...) the request is sent onto the script redirect.php

If redirect.php also accepts parameters and these are identified using a questionmark then you'll have a loop which is a thing to avoid.

Anyway, search for 301 redirect using the site search on top if you are starting from scratch, it's one of the subjects in here that has a lot of threads. You problem has been solved before, i'm sure.

/claus

forefront

7:24 pm on Aug 26, 2003 (gmt 0)

10+ Year Member



>>>If redirect.php also accepts parameters and these are identified using a questionmark then you'll have a loop which is a thing to avoid.

No, redirect.php will in itself pick up the REQUEST_URI and then do stuff.

We are changing a site that uses .cfm URIs possibly also with query strings and we want to send all old .cfm and/or URIs with query strings to their new /some/other/nice/uri.html counterpart - but there is no direct relationshuip between old.cfm?url=foo and new/nice.html urls so redirect.php will do a DB lookup based on the REQUEST_URI - which if it comntains .cfm and/or a query string we know it must be an old URI or a 404.

I am still working on this and will post a solution here but if anyone comes up with anything before then, I much appreciate the input.

Thanks.

claus

7:30 pm on Aug 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh, i think you can do it without mod_rewrite. Do you have custom error pages?

Make the custom 404 page your redirect.php and just delete all the old stuff, then it will catch the REQUEST_URI by default

/claus

forefront

7:50 pm on Aug 26, 2003 (gmt 0)

10+ Year Member



>>>oh, i think you can do it without mod_rewrite. Do you have custom error pages? Make the custom 404 page your redirect.php and just delete all the old stuff, then it will catch the REQUEST_URI by default

Naturally, that old trick works but the problem is that the web server throws a 404 status code and we want the redirection to be seamless so that after redirection to redirect.php, the user is displayed the URI they typed in, the new URI, there is a 10 second delay before redirection to the new URI and the redirect.php sets a 301 status code to the user's browser so search engine listings for old pages built over the years are not lost with a 404 but the search engines *should* update their listing with the 301/RedirectPermanent URI.

forefront

7:54 pm on Aug 26, 2003 (gmt 0)

10+ Year Member



So...here is what I have so far:


RewriteEngine on
RewriteCond %{REQUEST_URI}(.*)\.cfm$ ^/(except¦cgi-bin¦css¦images¦logs)/?.*$
RewriteRule ^(.+) - [L]
RewriteRule ^%{REQUEST_URI}(.*)\.cfm$ redirect.php [T=application/x-httpd-php,L]
RewriteRule ^%{REQUEST_URI}(.*)\.?$ redirect.php [T=application/x-httpd-php,L]

To recap we want:

* If the URI requested contains a question mark anywhere in the URI

AND/OR

* If the URI requested contains the filename extension .cfm anywhere in the URI

...then the request is sent onto the script redirect.php which does everything else.

...all other requested URIs including 404s are left alone.

I also want the rule to take effect whether the URI is "short" i.e. foo.com/page.cfm?var=23 or "long" foo.com/1/2/3/4/5/page.cfm?var=23 or foo.com/1/2/3/4/5/6/page.cfm without having to specify all the levels of directory structure within the rule.

Thanks.

jdMorgan

8:35 pm on Aug 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



forefront,

Redirect to /redirect.php if .cfm extension or non-blank query string:


RewriteEngine on
RewriteCond %{REQUEST_URI} \.cfm$ [OR]
RewriteCond %{QUERY_STRING} .
RewriteRule !^redirect\.php /redirect.php [T=application/x-httpd-php,L]

The negative pattern in the RewriteRule will prevent a loop. This is for use in a per-directory (.htaccess) context. For use in httpd.conf, add a leading slash to the pattern in the RewriteRule:

RewriteRule !^/redirect\.php /redirect.php [T=application/x-httpd-php,L]

Jim