Changed site, abandoned dynamic database, made static pages

Discuss practical ways rearrange URLs using mod_rewrite.

Changed site, abandoned dynamic database, made static pages

Postby up10ad » Sun Jan 25, 2009 8:38 am

My site used to run on a portal software package that used dynamic urls in the form sitename.com/index.php?page=pagename. I have abandoned the portal software and created static pages . I need a global rewrite rule because there are a number of other sites that link to my old dynamic pages and I want them to get the correct new static page.

I want to create a permanent rewrite rule that will tell visitors and SEs to use the new static pages instead of the previous dynamic pages; an external rule.

For example, a previous page might have been
Code: Select all
http://mysite.com/index.php?page=pagename

which I want to be rewritten to
Code: Select all
http://mysite.com/pagename.php

This seems fairly easy but when I try to do so in my .htaccess file in the site root it doesn't do anything. Here are a few different versions that I have tried, note that I only use ONE of these at a time but none seem to work.
Code: Select all
rewrite rule ^([^/]+)/index\.php\?page=(.+)$ $1/$2.php [R=301,L]

RewriteRule ^index\.php\?page=(.*)$  $1.php [R=301,L]

The first generates a server error, the second just doesn't change anything.

It seems this should be easy but... NOT. I am running a WAMP installation with Apache2. Any ideas from you experts out there?
up10ad
 
Posts: 4
Joined: Sun Jan 25, 2009 5:23 am

Postby richardk » Sun Jan 25, 2009 9:13 am

Query strings are matched with the %{QUERY_STRING} variable and RewriteConds.

For example
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?page=([^&]+)(&.*)?$ [NC]
RewriteRule ^([^/]+)/index\.php$ /$1/%2.php [R=301,L]


You can also do redirects like this with PHP which may be easier if you ave a lot of variables with values.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby up10ad » Sun Jan 25, 2009 10:06 am

Richard, thanks for the reply! I only have the single form of dynamic urls to convert so a single rule shoud do the trick. Unfortunately, the example you provided does not do anything. I'm not sure why. I know that my .htaccess file is working because I can add a really basic rule and it is fine. Any ideas why it fails? The page names are exactly as I indicated in my first message and both sets of pages still exist. The only difference is that the new 'home' page for the site is default.php so not to conflict with the old index.php. If I start with the new default.php all the menus, etc properly reference the new static pages fine. The issue again is the previous viewers or SEs that have links to the old dynamic pages.

Thanks again in advance.
up10ad
 
Posts: 4
Joined: Sun Jan 25, 2009 5:23 am

Postby richardk » Sun Jan 25, 2009 4:05 pm

Try replacing
Code: Select all
^([^/]+)/index\.php$

with
Code: Select all
^index\.php$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby up10ad » Sun Jan 25, 2009 5:03 pm

We're getting close! Now when I enter the url
Code: Select all
http://deltaweathercam.com/index.php?page=regionalsat

it translates it to the url
Code: Select all
http://deltaweathercam.com//regionalsat.php?page=regionalsat

Note the double // which is probably easy to correct, but also the appended ?page=regionalsat, which shouldn't be there.

I have tested and the double // is fixed by removing the / before the $1, but I can't seem to get rid of the ?page= on the replacement.
up10ad
 
Posts: 4
Joined: Sun Jan 25, 2009 5:23 am

Postby richardk » Sun Feb 01, 2009 6:14 am

? suppresses the query string.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?page=([^&]+)(&.*)?$ [NC]
RewriteRule ^index\.php$ /%2.php? [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby up10ad » Sun Feb 01, 2009 6:23 am

richardk wrote:? suppresses the query string.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?page=([^&]+)(&.*)?$ [NC]
RewriteRule ^index\.php$ /%2.php? [R=301,L]

That is perfect! Actually, with the previous help you gave I was able to come up with it a few days ago, but your reply confirmed my understanding. Thanks so much!
up10ad
 
Posts: 4
Joined: Sun Jan 25, 2009 5:23 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 26 guests

cron