homepage Welcome to WebmasterWorld Guest from 174.129.163.183
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Mod rewrite and redirecting old ugly urls
rwilson




msg:4508509
 5:49 pm on Oct 16, 2012 (gmt 0)

I want to use mod_rewrite on my old product urls however they have many links and decent page rank. How can I redirect the ugly url to the new rewritten one without creating an endless loop?

This is in my .htaccess, it creates a loop and crashes. Ultimately I want all these redirects to be handled in a database to avoid large amounts of code in .htaccess

RewriteRule ^products/([0-9]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ /products/index.php?id=$1 [NC,L]

rewritecond %{QUERY_STRING} ^id=6$
rewriteRule ^products/index.php$ http://www.example.com/products/6/example/product? [R=301,L]

 

rwilson




msg:4508512
 6:02 pm on Oct 16, 2012 (gmt 0)

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]


This seems to work, is there anything I should be worried about when using this?

g1smd




msg:4508543
 6:55 pm on Oct 16, 2012 (gmt 0)

The parameter URL to extensionless URL external redirect should test THE_REQUEST in a preceding RewriteCond. This ensures that only external requests with parameters are redirected, not those as a result of a previous internal rewrite.

The parameter to extensionless external redirect should be listed before the extensionless to parameter internal rewrite.

Escape literal periods in the external redirect RegEx pattern.

Do NOT use the NC flag in the internal rewrite ruleset otherwise you'll have a major Duplicate Content problem. Use all lower case for extensionless URLs.

rwilson




msg:4508565
 8:10 pm on Oct 16, 2012 (gmt 0)

I'm not sure I follow. Where do I use THE_REQUEST? I can remove the NC flag, how would that create duplicate content? Thanks.

g1smd




msg:4508570
 8:19 pm on Oct 16, 2012 (gmt 0)

Use THE_REQUEST with a RewriteCond to look at the actual URL request coming from the browser. There's thousands of prior example code snippets in this forum.

If example.com/ThisPage and example.com/thispage and example.com/ThIsPaGe all return the same content and all with '200 OK' status you have a Duplicate Content problem.

lucy24




msg:4508709
 1:50 am on Oct 17, 2012 (gmt 0)

Aw, heck, let's go for Duplicate Content. It's been a month or so:

The Redirect-to-Rewrite Two-Step

Problem: Your dynamically generated pages have long, ugly, hard-to-memorize URLs, probably containing query strings. You want them to have short pretty URLs.

The Solution comes in two parts.

Part 1. Redirect
When a user asks for the long ugly URL, redirect to the short pretty URL. Basic pattern:

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} queryname=([a-z]+)
RewriteRule longcomplicatedURL http://www.example.com/blahblah/%1? [R=301,L]


The %1 is captured from the original query string, and the final ? means that you now get rid of the query string. In real life it will usually be a little more complicated, but that's the basic process.

Example:
user asks for
www.example.com/directory/morestuff/index.php?model=volvo

They get bounced over to
www.example.com/cars/volvo

Part 2. Rewrite
You get an incoming request for a short pretty URL-- either from a new arrival or from someone who was redirected in Part 1. The server can't tell the difference.

RewriteRule blahblah/([a-z]+)$ longcomplicatedURL?queryname=$1 [L]

This time around, you're capturing part of the request and changing it into a query string.

Example:
user asks for
www.example.com/cars/volvo

They may think that's what they're getting-- it's what the browser's address bar says-- but behind the scenes the page content is really coming from
www.example.com/directory/morestuff/index.php?model=$1

Now you see why Part 1 had to look at THE_REQUEST. It's for insurance. If something happens later on, your long complicated URL might pass through mod_rewrite again. If it does, you need to be sure it doesn't get re-redirected. Otherwise there will be an infinite loop.

Now wait a minute! Does this mean that if someone starts out asking for "longcomplicatedURL", they go through this whole rigamarole and then they end up right back where they started?

Yup. But they don't know it. They only know what the browser's address bar tells them. Even robots-- yes, even google-- can't tell that they're being rewritten.

The Redirect part of the package-- Part 1-- is not technically necessary. The Rewrite-- Part 2-- will function without it. But redirecting everyone to the same URL means that everyone is now on the same page ... and it avoids nasty things like Duplicate Content.

But you're not done yet.

Part 0.
Before you do anything with Part 1 and Part 2, go over your current site carefully. Make sure that your own links point only to the short pretty URL. Requests for the long complicated URL should come only from outside-- from people with outdated bookmarks, or old links from other sites. Your own site will use only the pretty URLs.

rwilson




msg:4508964
 4:59 pm on Oct 17, 2012 (gmt 0)

Thank you guys! Lucy, you're answer was very helpful!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved