简体   繁体   中英

Create SEO-friendly URL with PHP with just the Querystring Value

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

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.

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.

Based on your example:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM