Forum Moderators: phranque

Message Too Old, No Replies

301 RewriteRule Format for Dynamic Pages

         

David_1cog

8:21 am on Mar 20, 2005 (gmt 0)

10+ Year Member



I've successfully implemented dynamic to static URLs for a site. I'm trying to externally redirect (301) for benefit of visitors and search engines. After several hours of reading here and elsewhere, I've run out of variables on how to format RewriteCond + RewriteRule.

This is one of my 'stabs in the dark':


Options +FollowSymLinks
RewriteEngine on
#
# If the client requests a dynamic URL, externally redirect the client to a static URL
RewriteCond %{QUERY_STRING} ^mainpage=([^&]+) [NC]
RewriteRule ^/index\.php$ /%1.html? [R=301,L]

The internal rewrites are as follows. I'm not expecting answers to all (although that would be very nice!), but a nudge in the right direction with a couple examples would be great. :)


RewriteBase /
RewriteRule ^skin-care/(.*)notify_remove\.html /index.php?main_page=product_info&products_id=$1&action=notify_remove [L]
RewriteRule ^skin-care/(.*)notify\.html /index.php?main_page=product_info&products_id=$1&action=notify [L]
RewriteRule ^skin-care/(.*)add\.html /index.php?main_page=product_info&products_id=$1&action=add_product [L]
RewriteRule ^skin-care/(.*)\.html /index.php?main_page=product_info&products_id=$1 [L]
RewriteRule ^skincare-range/(.*)/filter(.*)/page(.*)/sort(.*)\.html /index.php?main_page=index&cPath=$1&filter_id=$2&page=$3&sort=$4 [L]
RewriteRule ^skincare-range/(.*)/sort(.*)/filter(.*)/page(.*)\.html /index.php?main_page=index&cPath=$1&sort=$2&filter_id=$3&page=$4 [L]
RewriteRule ^skincare-range/(.*)/filter(.*)/sort(.*)/page(.*)\.html /index.php?main_page=index&cPath=$1&filter_id=$2&sort=$3&page=$4 [L]
RewriteRule ^skincare-range/(.*)/sort(.*)/filter(.*)\.html /index.php?main_page=index&cPath=$1&sort=$2&filter_id=$3 [L]
RewriteRule ^skincare-range/(.*)/sort(.*)/page(.*)\.html /index.php?main_page=index&cPath=$1&sort=$2&page=$3 [L]
RewriteRule ^skincare-range/(.*)/page(.*)/sort(.*)\.html /index.php?main_page=index&cPath=$1&page=$2&sort=$3 [L]
RewriteRule ^skincare-range/(.*)\.html /index.php?main_page=index&cPath=$1 [L]
RewriteRule ^order/(.*)\.html /index.php?main_page&action=buy_now&products_id=$1 [L]
RewriteRule ^(.*)/display(.*)_(.*)\.html /index.php?main_page=$1&disp_order=$2&page=$3 [L]
RewriteRule ^shippinginfo\.html /index.php?main_page=shippinginfo [L]
RewriteRule ^privacy\.html /index.php?main_page=privacy [L]
RewriteRule ^conditions\.html /index.php?main_page=conditions [L]
RewriteRule ^contact_us\.html /index.php?main_page=contact_us [L]
RewriteRule ^gv_faq\.html /index.php?main_page=gv_faq [L]
RewriteRule ^site_map\.html /index.php?main_page=site_map [L]
RewriteRule ^specials\.html /index.php?main_page=specials [L]
RewriteRule ^suppliers\.html /index.php?main_page=suppliers [L]
RewriteRule ^unsubscribe\.html /index.php?main_page=unsubscribe [L]
RewriteRule ^featured_products\.html /index.php?main_page=featured_products [L]
RewriteRule ^products_new\.html /index.php?main_page=products_new [L]
RewriteRule ^products_all\.html /index.php?main_page=products_all [L]
RewriteRule ^tell_a_friend/to_(.*)/products(.*)\.html /index.php?main_page=tell_a_friend&to_email_address=$1&products_id=$2 [L]
RewriteRule ^tell_a_friend/products(.*)\.html /index.php?main_page=tell_a_friend&products_id=$1 [L]
RewriteRule ^product_reviews/products(.*)\.html /index.php?main_page=product_reviews&products_id=$1 [L]
RewriteRule ^product_reviews\.html /index.php?main_page=product_reviews [L]
RewriteRule ^manufacturers_id(.*)\.html /index.php?main_page=index&manufacturers_id=$1 [L]
RewriteRule ^redirect/manufacturers_id(.*)\.html /index.php?main_page=redirect&action=manufacturer&manufacturers_id=$1 [L]
RewriteRule ^redirect_url-(.*) /index.php?main_page=redirect&action=url&goto=$1 [L]
RewriteRule ^login\.html /index.php?main_page=login [L]
RewriteRule ^logoff\.html /index.php?main_page=logoff [L]
RewriteRule ^account\.html /index.php?main_page=account [L]
RewriteRule ^shopping_cart\.html /index.php?main_page=shopping_cart [L]
RewriteRule ^checkout_shipping\.html /index.php?main_page=checkout_shipping [L]
RewriteRule ^advanced_search\.html /index.php?main_page=advanced_search [L]
RewriteRule ^popup_shipping_estimator\.html /index.php?main_page=popup_shipping_estimator [L]
RewriteRule ^index\.html.zenid=(.*)$ /index.php?main_page=index&zenid=$1 [L]
RewriteRule ^index\.html$ /index.php?main_page=index [L]
RewriteRule ^skincare-info/(.*)\.html /index.php?main_page=infopages&pages_id=$1 [L]

[edited by: jdMorgan at 4:55 pm (utc) on Mar. 20, 2005]
[edit reason] Fixed code formatting. [/edit]

jdMorgan

5:21 pm on Mar 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first thing I see is that you need to use "main_page", not "mainpage", to be consistent with what your other rules are doing.

# If the client requests a dynamic URL, externally redirect the client to a static URL
RewriteCond %{QUERY_STRING} ^mai[b]n_p[/b]age=([^&]+) [NC]
RewriteRule ^/index\.php$ /%1.html? [R=301,L]

The usual way to do what I think you're trying to do is to use %{THE_REQUEST}. This is a variable that is never updated after an internal rewrite. Unlike %{REQUEST_URI} or the URL-path "seen" by RewriteRule, it always contains the original client request. This is key to doing "two-way" rewrites, i.e. from static to dynamic for external requests for static URLS, and from dynamic to static for externally-requested dynamic URLs. If you try to use just those other variables, you can end up rewriting back and forth ad-infinitum.

# If the client directly requests a dynamic URL, externally redirect the client to a static URL
RewriteCond %{THE_REQUEST} ^GET\ /index\.php.*\?main_page=([^&]+) [NC]
RewriteRule ^/index\.php$ /%1.html? [R=301,L]

If I have missed your question, please answer the following to help us help you: What is working and what is not working? What have you done to test, and how do the results of that test differ from your expectations? Is your code looping? Are you seeing other errors listed in your server error logs? If so, what are they?

Jim

David_1cog

6:09 pm on Mar 20, 2005 (gmt 0)

10+ Year Member



The first thing I see is that you need to use "main_page", not "mainpage", to be consistent with what your other rules are doing.

To quote "Spinal Tap": It's such a fine line between stupid and clever.

Thanks for taking time to reply, Jim. I think I've read *all* your threads on this topic but still stabbing in the dark. Ho hum. Gonna be dreaming \/.*?^(a-Z)* tonight!

I tried the last statement group - still getting '200 OK' when visiting [mydomain.co.uk...]

My lack of comprehension may reduce lack of description, but I'm aiming to 301, e.g.:

index.php?main_page=shippinginfo -> shippinginfo.html

If a URL would help, happy to PM.

David.

jdMorgan

8:12 pm on Mar 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I forgot to show a canonical URL, and if you are running this code in .htaccess, remove the leading slash on the RewriteRule pattern:

# If the client directly requests a dynamic URL, externally redirect the client to a static URL
RewriteCond %{THE_REQUEST} ^GET\ /index\.php.*\?main_page=([^&]+) [NC]
RewriteRule [b]^in[/b]dex\.php$ [b]http://www.example.co.uk/[/b]%1.html? [R=301,L]

If it works, you should be able to dial your site up to Google PR11. You know, most sites only go up to PR10...

Jim

David_1cog

3:38 am on Mar 21, 2005 (gmt 0)

10+ Year Member



Hmmm... that gives a 404 (The requested URL /shippinginfo HTTP/1.1.html was not found on this server.) as is. Removing the leading:

# If the client directly requests a dynamic URL, externally redirect the client to a static URL
RewriteCond %{THE_REQUEST} ^GET\ index\.php.*\?main_page=([^&]+) [NC]
RewriteRule ^index\.php$ http://www.example.co.uk/%1.html? [R=301,L]

slash still gives '200'

Trying to think of a witty 'spontaneous combustion' response, but that's meeting with same amount of success as my [code] Style Code being applied. :/

[edited by: jdMorgan at 4:00 am (utc) on Mar. 21, 2005]
[edit reason] Examplified. [/edit]

jdMorgan

4:07 am on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, one more try... That code did what I said, not what I meant:

# If the client directly requests a dynamic URL, externally redirect the client to a static URL
RewriteCond %{THE_REQUEST} ^GET\ index\.php.*\?main_page=([^&]+)[b].*\ HTTP[/b] [NC]
RewriteRule ^index\.php$ http://www.example.co.uk/%1.html? [R=301,L]

The WebmasterWorld [code] tag must be closed with [/code], not [\code].

Jim

David_1cog

4:46 am on Mar 21, 2005 (gmt 0)

10+ Year Member



Nope. Still 200: [MYDOMAIN.co.uk...]

I am actually trying to understand the process and not just waiting to be spoon-fed, although it's still a bit hieroglyphic. Something puzzles me about your rule: in the RewriteCond, why the '.*' in 'index\.php.*\?main_page=('? There's never going to be '1 or many characters' at that point?

[sigh] [/code] / [\code] - we're back to that fine line again.

jdMorgan

5:05 am on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Some days, I just should stay away from the keyboard...

# If the client directly requests a dynamic URL, externally redirect the client to a static URL
RewriteCond %{THE_REQUEST} ^GET\ [b]/i[/b]ndex\.php\?main_page=([^&]+).*\ HTTP [NC]
RewriteRule ^index\.php$ http://www.example.co.uk/%1.html? [R=301,L]

Yes, the ".*" was superfluous, and now I can't remember what I was thinking. But the leading slash in the RewriteCond is required and was missing.

As to understanding, the patterns have to match the request exactly, and because I fat-fingered the code, they didn't.

Jim

David_1cog

5:29 am on Mar 21, 2005 (gmt 0)

10+ Year Member



Bingo! :)

However (there had to be, of course), some multi-part URLs (e.g. [MYDOMAIN.co.uk...] now kick back to index.html, while others (e.g. [MYDOMAIN.co.uk...] 404.

I'm thinking all the rewrites I originally listed are going to take some fancy regexing. <snip>

[edited by: jdMorgan at 5:49 am (utc) on Mar. 21, 2005]