Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule causing 301 redirect issue

RewriteRule and 301 Redirect

         

csherwood1234

10:24 pm on Nov 19, 2007 (gmt 0)

10+ Year Member



Here's the problem.

I had a page which was dynamically generated for years with the URL, [mydomain.com...]

The page had a PR of 3.

However, I've now created a permanent server side redirect (a 301 redirect) to a new static HTML page that I've created to replace the old one. I've done 301 redirects on a number of other sites from one static page to another, and this has always worked great and preserved the PageRank.

The problem I'm encountering is that I can't figure out how to setup a RewriteRule and/or 301 redirect so that it properly redirects the old dynamic page's URL to my new static page.

This is what my .htaccess file looks like:

redirect 301 /cat--Wine-Gift-Baskets-Champagne-Beer--WINE [mydomain.com...]

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^shop_by_price--(.*) /cgi-bin/ccp51/cp-app.cgi?seo=shop_by_price--$1
RewriteRule ^cat--(.*) /cgi-bin/ccp51/cp-app.cgi?seo=cat--$1
RewriteRule ^item--(.*) /cgi-bin/ccp51/cp-app.cgi?seo=item--$1
RewriteRule ^page--(.*) /cgi-bin/ccp51/cp-app.cgi?seo=page--$1
RewriteRule ^store /cgi-bin/ccp51/cp-app.cgi?pg=store
#RewriteRule ^$ /cgi-bin/ccp51/cp-app.cgi RewriteRule ^index$ /cgi-bin/ccp51/cp-app.cgi?pg=ste_index_list
RewriteRule ^az-(.)-(.)$ /cgi-bin/ccp51/cp-app.cgi?pg=ste_index_az&startltr=$1&endltr=$2
RewriteRule ^sitemap.xml$ /cgi-bin/ccp51/cp-app.cgi?pg=ste_sitemap_proc

While my redirect DOES in fact redirect the old URL to the new one, I get this as the URL:

[mydomain.com...]

instead of this:

[mydomain.com...]

Consequently, my PR is not transferring.

I know that the screwed-up URL is a result of the Rewrite rule, but I don't know how to get around this.

I'm thinking that I can probably EXCLUDE the [mydomain.com...] URL with a RewriteCond but am unsure how to code this. I'm thinking something like this:

redirect 301 /cat--Wine-Gift-Baskets-Champagne-Beer--WINE [mydomain.com...]

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^shop_by_price--(.*) /cgi-bin/ccp51/cp-app.cgi?seo=shop_by_price--$1

RewriteCond {REQUEST_URI}!^/cat--Wine-Gift-Baskets-Champagne-Beer--WINE
RewriteRule ^cat--(.*) /cgi-bin/ccp51/cp-app.cgi?seo=cat--$1

RewriteRule ^item--(.*) /cgi-bin/ccp51/cp-app.cgi?seo=item--$1
RewriteRule ^page--(.*) /cgi-bin/ccp51/cp-app.cgi?seo=page--$1
RewriteRule ^store /cgi-bin/ccp51/cp-app.cgi?pg=store
#RewriteRule ^$ /cgi-bin/ccp51/cp-app.cgi RewriteRule ^index$ /cgi-bin/ccp51/cp-app.cgi?pg=ste_index_list
RewriteRule ^az-(.)-(.)$ /cgi-bin/ccp51/cp-app.cgi?pg=ste_index_az&startltr=$1&endltr=$2
RewriteRule ^sitemap.xml$ /cgi-bin/ccp51/cp-app.cgi?pg=ste_sitemap_proc

Any help you can provide would be immensely appreciated!

jdMorgan

11:06 pm on Nov 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll only address two of your existing rules, because any more would just be too much like work :)

# Externally redirect dynamic URLs to static URLs only when requested by the client
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /cgi-bin/ccp51/cp-app\.cgi\?seo=shop_by_price--([^\ ]*)\ HTTP/
RewriteRule ^cgi-bin/ccp51/cp-app\.cgi$ http://www.example.com/shop_by_price--%1 [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /cgi-bin/ccp51/cp-app\.cgi\?seo=cat--([^\ ]*)\ HTTP/
RewriteRule ^cgi-bin/ccp51/cp-app\.cgi$ http://www.example.com/cat--%1 [R=301,L]
#
# Internally rewrite static URLs to dynamic script filepath and query
RewriteRule ^shop_by_price--(.*)$ /cgi-bin/ccp51/cp-app.cgi?seo=shop_by_price--$1 [b][L][/b]
RewriteRule ^cat--(.*)$ /cgi-bin/ccp51/cp-app.cgi?seo=cat--$1 [b][L][/b]

THE_REQUEST is the entire request header as received from the client (browser or 'bot), and for the first rule above, might look like:
GET /cgi-bin/ccp51/cp-app.cgi?seo=shop_by_price--foo HTTP/1.1

Pay particular attention to the regex character escaping in these rules -- literal ".", "?" and space characters must be escaped by preceding them with a "\".

Also, note that for the dynamic URL redirects, it may be possible to use one rule to redirect all of them if the friendly URL is always exactly the same as the "seo=" value. Example:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /cgi-bin/ccp51/cp-app\.cgi\?seo=([^\ ]+)\ HTTP/
RewriteRule ^cgi-bin/ccp51/cp-app\.cgi$ http://www.example.com/%1 [R=301,L]

The trick with the redirects here is that by checking the client request, we can be sure we are redirecting a dynamic URL only when it is requested by a client, and not when it is generated as a result of your internal rewriting rules. Unfortunately, this requires a bit of redundancy in the RewriteRule and RewriteCond patterns.

Jim