Forum Moderators: phranque

Message Too Old, No Replies

rewrite all dynamic pages to homeq

         

twent12

10:30 am on Feb 13, 2011 (gmt 0)

10+ Year Member



Hi,

I need to rewrite all dynamic pages to home.

An eg url would be

www.domain.com/content/normal.asp?name=widget

I need all /content/normal.asp?name=* pages to go to /

How can i do this please?

Thanks a lot.

g1smd

1:04 pm on Feb 13, 2011 (gmt 0)

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



There are tens of thousands of previous threads here.

Many contain code for, and explain how to, redirect.

What code have you tried?

twent12

11:17 pm on Feb 13, 2011 (gmt 0)

10+ Year Member



Hi G1smd,

yes I appreciate that - I have tried many variations, but the best I can do always has the ?name=widget at the end,

ie the page redirects to www.domain.com/?name=widget

it is the getting rid of that bit that I am struggling with.

A couple of variations I have tried:


RewriteCond %{QUERY_STRING} ^name\=([^&]+)$
RewriteRule ^$ / [R=301,L]

RewriteRule \.asp$ / [R=301,L]

g1smd

11:26 pm on Feb 13, 2011 (gmt 0)

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



OK. Simple to solve that.

The redirect target needs all of these things added:
- the protocol and domain name
- a trailing question mark to clear the query string data.

In place of
/
you need
http://www.example.com/?
in both rules.

Be aware that if the query has other parameters before or after "name" then the redirect will not be actioned.


You also need to add the standard non-www to www redirect code after the above:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule .* http://www.example.com/$1 [R=301,L]

twent12

11:47 pm on Feb 13, 2011 (gmt 0)

10+ Year Member



Doesn't work.

ok, my bad sorry, I should have said that the original .asp is on one domain, and the redirect to another (using php).

The new site itself has dynamic pages though using a seperate rule.

g1smd

11:50 pm on Feb 13, 2011 (gmt 0)

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



Clear your browser cache before testing.

Use the "Live HTTP Headers" extension for Firefox to examine browser requests and server responses.

"Doesn't work" conveys no useful meaning in helping to work out what actually happened.

twent12

12:04 am on Feb 14, 2011 (gmt 0)

10+ Year Member



sorry. It is a redirect loop. I am using firefox 4, and live headers is not available. I will find a version 3 somewhere and install.

thanks.

twent12

12:07 am on Feb 14, 2011 (gmt 0)

10+ Year Member



Full ruleset is here (if this helps):

RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.domain_OLD\.com)?$
RewriteRule .* [domain.com...] [R=301,L]

RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteRule ^(.*)$ [domain.com...] [R=permanent,L]


#redirect 301 /blog /blog/
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule ^blog$ /blog/ [L,R=301]

# page specific rss rules
RewriteRule ^flex-([a-z0-9-_:]+)-rss.xml$ rss.php?type=flex-page&id=$1 [L]
RewriteRule ^blog-([a-z0-9-_:]+)-rss.xml$ rss.php?type=blog-page&id=$1 [L]
RewriteRule ^products-([a-z0-9-_:]+)-rss.xml$ rss.php?type=product-category-page&id=$1 [L]
RewriteRule ^product-([a-z0-9-_:]+)-rss.xml$ rss.php?type=product-page&id=$1 [L]

# collection rss rules
RewriteRule ^rss.xml$ rss.php?type=flex [L]
RewriteRule ^product-rss.xml$ rss.php?type=product [L]
RewriteRule ^blog-rss.xml$ rss.php?type=blog [L]
RewriteRule ^all-rss.xml$ rss.php?type=all [L]

RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule ^blog/archive-([0-9-]+) blog.php?m=$1&id=7 [L,QSA]

RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule ^blog/([0-9]+) blog.php?page=$1&id=7 [L,QSA]

RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule ^blog blog.php?id=7 [L,QSA]

RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule ^tag/([a-z0-9-]+) tag.php?t=$1 [L,QSA]



RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !//
RewriteRule (.*)/(.*)/(.*)/(.*) $1.php?id=$3&page=$4&name=$2 [L,QSA]

g1smd

12:54 am on Feb 14, 2011 (gmt 0)

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



Correct the code in the first block exactly as per the example above. Especially remove _OLD from it.

You first rule currently reads: "If request is not for OLD, redirect to new". So it redirects to new. The rule runs again, and since new is not OLD it redirects to new. The rule runs again, and since new is not OLD it redirects to new. The rule runs again, and since new is not OLD it redirects to new. Forever.


Delete the second code block. It is not needed once you have fixed the first ruleset.


Next, you need to move the /blog/ redirect so that it is before the non-www redirect.

The /blog/ redirect needs to include the domain name in the redirect target.

Do both of the above to avoid having a multi-step redirection chain for non-www requests without the trailing slash.


You have a mix of [R=permanent,L] and [R=301,L] and [L,R=301] on your redirects.

While they all do the same thing, the code is much easier to read if you use [R=301,L] every time.



The
(.*)/(.*)/(.*)/(.*)
pattern is very inefficient. It takes thousands of back off and retry trial matches to find a match. Replace it with
([^/]+)/([^/]+)/([^/]+)/([^.]+)
or similar.

As coded now, a request for
example.com////
would be treated as valid by your final rule.

In that final rule add a / before the $1 to avoid a nasty problem that can happen sometimes.

twent12

1:22 am on Feb 14, 2011 (gmt 0)

10+ Year Member



you're a star.. I wil try this out shortly.

Thanks g1.