Forum Moderators: phranque

Message Too Old, No Replies

htaccess to rewrite dynamic URLs

         

wfco

2:26 pm on May 5, 2011 (gmt 0)

10+ Year Member



I hope someone can help me with these questions. Thanks in advance for reading!

1. My CMS can automatically create http://www.example.com/content.php?sid=282, but I want users to link to (and see) http://www.example.com/Cool-Page. I added the following line into .htaccess

RewriteRule ^Cool-Page$ http://www.example.com/content.php?sid=282 [R=301,L]

The problem is, whenever someone goes to /Cool-Page, the URL in the address bar still shows content.php?sid=282 instead of /Cool-Page. Is there any way to fix this in an SEO friendly matter?

2. I have several ways to describe a single page. For example, a product might fall into 2 categories:

http://www.example.com/Cat-1/Product.html and
http://www.example.com/Cat-2/Product.html

My CMS automatically creates the second Cat-2/Product.html page based on a product being added into 2 categories. Can htaccess help here to get rid of the second (duplicate content page) or should I fix the URL problem by removing the /Cat-2/Product.html directly?

g1smd

7:54 pm on May 5, 2011 (gmt 0)

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



You must link to the URL that you want users to see and use. URLs are defined in links.

After you link to the new URL you need two things.

One is a redirect from old URL to new URL. It is there merely for users asking for the old URL after clicking a link in a stale SERP or in their bookmarks.

You also need a rewrite so that when the new URL is requested the server fetches content from from the old server filepath. Note that it fetches it from the old server filepath, not from the old URL.

URLs are used out on the web and contain a domain name. Server filepaths are used inside the server and do not include a domain name. URLs and server filepaths are two different things. They are merely associated by the action of a server.

You need to reverse the direction of your redirect and replace the current redirect with a rewrite.

The new external redirect:
RewriteCond %{HTTP_HOST} ^[A-Z]{3,9}\ ^/content\.php\?sid=282\ HTTP/
RewriteRule ^content\.php$ http://www.example.com/Cool-Page? [R=301,L]


The new internal rewrite:
RewriteRule ^Cool-Page$ /content.php?sid=282 [L]


As for the category duplication, it can be solved in several ways. The cheapskate way is to add the rel="canonical" tag to both pages, where both reference "cat1". Another is to redirect requests for that page in "cat2" to the real page in "cat1". It would be far better to fix the entire system to not reference category in URL at all.

Change the page URLs to no longer include category data in the URL. Link to the page from whatever categories the page appears in. On that page link back to all the categories the page appears in.

[edited by: g1smd at 8:15 pm (utc) on May 5, 2011]

wfco

8:04 pm on May 5, 2011 (gmt 0)

10+ Year Member



Wow, great answer! Worked like a charm. Any suggestions for question #2?