0

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

1
  • Note: if you want URLs like this you cannot differentiate other kind of urls like restaurant.php?id=newyork so your apparoach is working only when you want to display shops only. Commented Aug 12, 2010 at 13:30

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

Based on your example:

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

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

2 Comments

Thank you. This works perfectly because the domain.com and domain.com/about.php do not redirect to 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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.