Forum Moderators: phranque

Message Too Old, No Replies

Redirecting from old pagename format to new

SE queries are hitting my old page names

         

VectorJ

9:05 pm on May 12, 2004 (gmt 0)

10+ Year Member



I recently reengineered my site so that, for example, a page named:

[example.com...]
is now
[example.com...]
and
[example.com...]
has similarly become
[example.com...]

The "view" parameter in the new scheme is always the name of the page in the old scheme minus the '.cgi' part. I figure I need a mod_rewrite script to redirect people, but I have some sort of bizarre mental block against being able to use mod_rewrite properly and my scripts always end up throwing errors or impregnating the cat or something. Any help would be much appreciated.

winglian

9:24 pm on May 12, 2004 (gmt 0)

10+ Year Member



I have/had the same problem. What I did was check the logs to see what old links were coming in and placed these in a .htoldlinks file.


/about.cgi /buildpage.cgi?view=about
/opinion.cgi /buildpage.cgi?view=opinion

This works best when there is no rhyme or reason the the filenames and there are just a few.
You must also include in the httpd.conf


RewriteEngine On
# check for those with query strings
RewriteMap oldlinks txt:/var/www/.htoldlinks
RewriteCond %{QUERY_STRING}!=""
RewriteCond %{REQUEST_URI}!=""
RewriteCond ${oldlinks:%{REQUEST_URI}?%{QUERY_STRING}¦NOT-FOUND}!=NOT-FOUND
RewriteRule ^.* ${oldlinks:%{REQUEST_URI}?%{QUERY_STRING}}? [R=301,L]

# check for those without query strings
RewriteCond %{REQUEST_URI}!=""
RewriteCond ${oldlinks:%{REQUEST_URI}¦NOT-FOUND}!=NOT-FOUND
RewriteRule ^.* ${oldlinks:%{REQUEST_URI}} [R=301,L]

This is what works for me as I have a variety of static pages and old cfm pages with queries as well as directory structures that I have migrated. Your mileage may vary.

gergoe

10:11 pm on May 12, 2004 (gmt 0)

10+ Year Member



maybe i did not catch the point, but it sounds to me that you want to redirect *all* the requests for cgi pages (except the buildpage.cgi) to this new one? how much cgi you have there approx?

VectorJ

5:50 am on May 13, 2004 (gmt 0)

10+ Year Member



winglian: Thanks for the code! I'll probably end up using that if I can't find some way of extracting the page name and sticking it onto the end of the buildpage.cgi page. Nice to know there are options.

gergoe: Essentially all of the pages are being redirected to buildpage.cgi, but with "?view=somepage" appended to the end. Ideally what I'd like to be able to do is take any page (call it "oldpage.cgi") that isn't buildpage.cgi, extract the cgi name, strip off the ".cgi" at the end, and redirect it to buildpage.cgi?view=oldpage.

grandpa

6:42 am on May 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you can't access httpd.conf then maybe something like this would work.

RewriteCond %{REQUEST_URI} ^/buildpage.cgi?view=(*.)$ [NC]
RewriteRule ^buildpage\.cgi$ http://www.example.com/buildpage.cgi?view=$1 [L]

RewriteCond %{REQUEST_URI} (/*.cgi)$ [NC]
RewriteRule (*.)\.cgi$ http://www.example.com/buildpage.cgi?view=$1 [L]

The second rule would be used to rewrite any requests for anyfile.cgi. The first rule is intended to prevent rewriting any request for buildpage.cgi, or, more accurately, to rewrite to iself, thus avoiding the second rule.

I'm no expert, and offer no claims of usability. But as a simple solution it might work.

gergoe

9:17 am on May 13, 2004 (gmt 0)

10+ Year Member



then you can use the following:

RewriteEngine on
RewriteCond %{REQUEST_URI}!^/buildpage.cgi [NC]
RewriteRule ^/(.+)\.cgi$ /buildpage.cgi?view=$1 [QSA,R,L]

this should redirect every request for /anyscript_except_buildpage.cgi to /buildpage.cgi?view=anyscript. please note that putting this into a htaccess file might requires a RewriteBase directive and the removal of the leading slash from the RewriteRule (if i know well; i did not use the .htaccess files much).

VectorJ

12:37 pm on May 13, 2004 (gmt 0)

10+ Year Member



Fantastic! Thanks gergoe and grandpa.

VectorJ

6:31 pm on May 13, 2004 (gmt 0)

10+ Year Member



After using the solutions you guys gave me, it finally occurred to me that I do have one or two pages that shouldn't be translated to the buildpage format. What I ended up doing is checking to see if the template file exists, then if it does rewriting the URI. I thought I'd share the mod_rewrite code just in case it might benefit anyone. The code is a cross between what you guys on here taught me and some code I stole from the URL Rewriting Guide at apache.org:

RewriteEngine on
RewriteCond %{REQUEST_URI}!buildpage.cgi [NC]
# parse out basename, but remember the fact
RewriteRule (.*)\.cgi$ $1 [C,E=WasCGI:yes]
# rewrite to buildpage.cgi if template exists
RewriteCond /home/example/example.com/templates/$1.txt -f
RewriteRule ^(.*)$ buildpage.cgi?view=$1 [S=1]
# else reverse the previous rewrite
RewriteCond %{ENV:WasCGI} ^yes$
RewriteRule ^(.*)$ $1.cgi [L]

Thanks again everyone for getting me on the right track.