Forum Moderators: phranque

Message Too Old, No Replies

redirecting url with variables to SEO friendly url with htaccess

htaccess redirect rewrite

         

reyez

8:32 am on Sep 29, 2007 (gmt 0)

10+ Year Member



I've got a problem with the following.

On an existing website we've changed the urls for SEO.

Now the urls look like this:

http://www.example.com/mainnavigation/subnavigation/title_of_the_page.html

The old situation looked like this:

http://www.example.com/?id=1 etc.

How do we redirect/rewrite (?) the old links to the new pages?

So http://www.example.com/?id=1 has to go to http://www.example.com/mainnavigation/subnavigation/title_of_the_page.html

NB: the destinationpage above is static!

This rule doesn't work:
Redirect 301 http://www.example.com/?id=1 http://www.example.com/mainnavigation/Aboutsubnavigation/title_of_the_page.html

We have the folowing rules in htaccess:

RewriteRule ^([^/\.]+)/([^/\.]+).html?$ index.php?idname=$1
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?idname=$1&pagename=$2
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+).html$ index.php?idname=$1&pagename=$2

[edited by: jdMorgan at 2:23 pm (utc) on Sep. 29, 2007]
[edit reason] example.com [/edit]

jdMorgan

2:10 pm on Sep 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to use mod_rewrite to test query strings, and you'll also need to test THE_REQUEST to prevent this new rule from creating an infinite loop by interacting with your existing static->dynamic rewrites.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?id=1\ HTTP/
RewriteRule ^$ http://example.site.com/mainnavigation/Aboutsubnavigation/title_of_the_page.ht[b]ml?[/b] [R=301,L]

If you have only a few dozen pages, using this code construct on a per-page basis is reasonable. However, if you have hundreds of pages, your .htaccess is going to get bloated and slow.

THE_REQUEST is the entire client request header, which for example, might look like this:

GET /some-page.php?arg1=abc&arg2=def HTTP/1.1

The question mark at the end of the rewriterule substitution URL will clear the requested query string. It will not appear in the rewritten URL itself.

Jim