views:

63

answers:

3

Is there a way use mod_rewrite to produce the result below?

Original URL: http://www.domain.com/shop.php?id=newyork

to

SEO friendly URL http://www.domain.com/newyork

I've seen plenty of example where the above URL can be converted to http://www.domain.com/shop/newyork but I actually don't want to display the word 'shop/' so just http://www.domain.com/newyork

+2  A: 

I'd have a go with something like the following, off the top of my head

RewriteRule ^([a-zA-Z]*)$ www.example.com/shop.php?id=$1

Do bear in mind that anything after your root domain, will be piped into your shop.php script.

DavidYell
A: 

Yes, in your .htaccess file put

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^/([^/.]+)$ shop.php?id=$1 [L]
</IfModule>

([^/.]+) will match anything that isn't a / or . and store that info, $1 at the end outputs that info into your shop script. [L] tells mod_rewrite to stop looking for rules if this one works.

JKirchartz
A: 

Based on your example:

RewriteRule ^([a-z]+)$ /shop.php?id=$1 [L]

Would match newyork, alaska, hamburg but not highway-1.

Florian Grell
Thank you. This works perfectly because the http://www.domain.com/ and http://www.domain.com/about.php do not redirect to http://www.domain.com/shop.php?. Because I have the list of all the pages other than shop, I'm thinking to write it out like this RewriteRule ^(about)$ /about.php RewriteRule ^(contact)$ /contact.php RewriteRule ^([a-zA-Z0-9]+)$ /page.php?id=$1. Is this the right way to do this? To detect first for any of the pages before calling the 'shop' rewrite.
blue
Yes, I think this could work. The only problem I see is this rule `RewriteRule ^([a-zA-Z0-9]+)$ /page.php?id=$1` because it is, more or less the same as `RewriteRule ^([a-z]+)$ /shop.php?id=$1 [L]`I would asume the your `shop` rule will never be called, because it will be matched first by your `page` rule.And make sure you add the `[L]`, which stands for last rule, after each `RewriteRule`.
Florian Grell