Forum Moderators: phranque

Message Too Old, No Replies

Redirecting Urls via .htaccess

         

seonick

2:20 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



I've successfully re-written our dynamic product urls using the following code:

RewriteRule (.*)-(.*)\.htm$ /proddetail.php?prod=$1&cat=$2

Now my question is, how do I re-direct the currently indexed urls to their new static versions?
For example:
somedomain.com/proddetail.php?prod=7&cat=4
==> Need to redirect to: somedomain.com/7-4.htm

Ideas?

jdMorgan

4:26 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a bit tricky, but do-able.

See the example and discussion at [webmasterworld.com...]

Jim

seonick

2:18 am on Nov 3, 2005 (gmt 0)

10+ Year Member



Thanks - I've tried a few things, but nothing seems to be working quite right. So far I've tried:
RewriteRule ^proddetail\.php\?prod\=([0-9]+)\&cat\=([0-9]+)$ $1-$2.htm [R=301,L]

RewriteCond %{QUERY_STRING} ^prod\=([^&]+)\&cat\=([^&]+)$
RewriteRule ^$ %1-%2.htm [R=301,L]

Again, I'm trying to get the dynamic URLs:
/proddetail.php?prod=1&cat=2
To redirect to their static versions:
/1-2.htm

jdMorgan

2:36 am on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You *must* use {THE_REQUEST} to do this, as shown in the cited thread. Otherwise, your two rules will each rewrite/redirect to each other, causing an 'infinite' redirection loop.

# Rewrite static URLs to dynamic
RewriteRule ([^-]+)-([^.]+)\.htm$ /proddetail.php?prod=$1&cat=$2 [L]
#
# Redirect only client-requested dynamic URLs to static
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /proddetail\.php\?prod=([^&]+)&cat=([^&]+)
RewriteRule ^proddetail\.php$ http://www.example.com/%1-%2.htm [R=301,L]

Jim

seonick

11:37 am on Nov 3, 2005 (gmt 0)

10+ Year Member



Alright - Now I have:

# Rewrite static URLs to dynamic
RewriteRule ([^-]+)-([^.]+)\.htm$ /proddetail.php?prod=$1&cat=$2 [L]
#
# Redirect only client-requested dynamic URLs to static
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /proddetail\.php\?prod=([^&]+)&cat=([^&]+)
RewriteRule ^proddetail\.php$ http://www.domain.com/%1-%2.htm [R=301,L]

Which lets the static urls render properly, but the dynamic urls now coming up with an error. This time the error comes out in the url
(which goes to a 404 page):
domain.com/27-12%20HTTP/1.1.htm?prod=27&cat=12

jdMorgan

2:40 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops...

It's picking up the "space and HTTP/1.1" tail of THE_REQUEST.

Change the rewritecond to:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /proddetail\.php\?prod=([^&]+)&cat=([^&\ ]+)

THE_REQUEST is the entire request line from the browser, for example,

GET /proddetail.php?prod=7&cat=4 HTTP/1.1

Since I forgot to stop the pattern-match on the space, it just kept going, since it didn't find an ampersand and another variable after cat=4.

JIm

seonick

5:16 pm on Nov 3, 2005 (gmt 0)

10+ Year Member



Now we're getting closer...

For some reason the url still contains the dynamic variable at the end, example:
proddetail.php?prod=7&cat=13
now redirects to:
/7-13.htm?prod=7&cat=13

using this code:

# Rewrite static URLs to dynamic
RewriteRule ([^-]+)-([^.]+)\.htm$ /proddetail.php?prod=$1&cat=$2 [L]
#
# Redirect only client-requested dynamic URLs to static
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /proddetail\.php\?prod=([^&]+)&cat=([^&\ ]+)
RewriteRule ^proddetail\.php$ http://www.domain.com/%1-%2\.htm [R=301,L]

Should redirect to just: /7-13.htm (without the? mark and query string)

Thanks for all the help!

jdMorgan

5:31 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sometimes, I should just stay away from the keyboard... ;)

Put a question mark at the end of the substitution URL to clear the current query string:


RewriteRule ^proddetail\.php$ http://www.domain.com/%1-%2\.ht[b]m?[/b] [R=301,L]

Jim

seonick

2:33 am on Nov 4, 2005 (gmt 0)

10+ Year Member



Worked like a charm!