views:

202

answers:

5

Hi! I'm new to mod_rewrite and need to do something for my client.

Suppose I have the www.mydomain.com/products.php?prod_id=32.

This product has a section (clothes) and a name (shirt). These names and sections are unique.

In a SEO-Friendly Url, it should be www.mydomain.com/products/clothes/shirt/.

I know I can create

RewriteRule ^products/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ products.php?section=$1&name=$2

I can do that, and it works. But I want people who enter www.mydomain.com/products.php?prod_id=32 to be redirected to www.mydomain.com/products/clothes/shirt/ (changed in the browser itself). How can I do that without inserting the id in my url? Is it possible to call a "pre-processing" php file in my .htaccess, and recreate "products.php?section=$1&name=$2"?

Anyone has a good link with really detailed explanation of mod_rewrite?

Thanks!

+4  A: 

You may have a bigger problem than mod_rewrite can handle gracefully. You can also use PHP to return a redirect to the browser. That way you can access your database to figure out that product_id 32 is /clothes/shirts/

Ned Batchelder
I think I understood your solution: create a regex that matches "products.php?prod_id=32", that redirects to "fix_old_url.php?prod_id=32" for example. Inside fix_old_url.php, I extract the "clothes" and "shirts" from a query using this id. After this I redirect to /clothes/shirts/ in php. Is there a difference from a SEO perspective in using a PHP redirect instead of an apache redirect?
Somebody still uses you MS-DOS
The browser (or search robot) will see the same thing from either redirect. You can't tell from outside the server how you implemented it.
Ned Batchelder
I'm trying to understand the flow too... suppose it's a user. browser -> products.php?prod_id=32 -> apache -> fix_old_url.php -> browser -> apache -> /products/clothes/shirt/ -> products.php?prod_id=32. Is it correct?
Somebody still uses you MS-DOS
I don't understand your last step. Why would /products/clothes/shirt/ go to products.php?prod_id=32. That would start the whole cycle again and you'd have an infinite loop. I thought you had a native handler for /products/clothes/shirt.
Ned Batchelder
+3  A: 

I see no other option than doing it inside PHP.

You can add something to the top of your products.php page that checks the URL ($_SERVER['REQUEST_URI']) to see if it contains products.php - If it does, redirect the person. You'll need to query your database to find out the product category and the name before redirecting though.

Remember to set the Moved Permanently header to improve SEO further :)

phidah
Thanks for the input. I would like to avoid inserting redirecting logic in my php files, I think Ned Batchelder's approach is better. Thanks!
Somebody still uses you MS-DOS
A: 

This data might be passed by the browser via the "Referer" header field. You could progress that url and look at the get arguments. If I remember right, this isn't supported on all browsers.

Kendall Hopkins
Why would the referer header contain this information?
phidah
A: 

Apache? Try an .htaccess redirect. No mod_rewrite needed.

Example: Redirect 301 /oldpage.html http://www.example.com/newpage.html

inked
What about the parameters?
phidah
Meh. I'll give you a -1 for using an emoticon.
inked
.htaccess takes more CPU resource and slower than configuring in the server side mod_rewrite.
Murvinlai
Murvinlai - that's a better response. Anything to keep resources low is a good thing.
inked
A: 

at the top of products.php (or any page you want to redirect) you could put a function call

redirectToSeoUrl();

Then in one of your include files write a redirectToSeoUrl function that gets the new url and redirects. Make sure to put the code before anything is output to the browser.

Galen