简体   繁体   中英

Php htaccess RewriteRule how to fix

i get the $_GET['id'] from url to show a page, to htaccess i have the follow code to show like this shop/123 and not shop.php?id=123

    RewriteEngine on
RewriteRule shop/(.*)/ shop.php?id=$1
RewriteRule shop/(.*) shop.php?id=$1

My problem is .. If i add to url shop/123/test/test/test this still working again and i have problems with the share links etc.

How i can fix it to takes only one parametes else to go to 404?

Sorry for my English

You can use this :

RewriteEngine on
RewriteRule ^shop/([^/]+)$ /shop.php?id=$1 [L]

This will rewrite /shop/foobar to /shop.php?id=foobar, but will not rewrite /shop/foobar/ with a traling slash or extra path-info .

If id is only numerical, you can change the rewrite rule to this:

RewriteEngine on
RewriteRule shop/(\d+)/ shop.php?id=$1
RewriteRule shop/(\d+) shop.php?id=$1

If you also have alphabets in id :

RewriteEngine on
RewriteRule shop/([a-zA-Z\d]+)/ shop.php?id=$1
RewriteRule shop/([a-zA-Z\d]+) shop.php?id=$1

Try this:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    </IfModule>

   RewriteRule ^shop/([0-9]+)/(.*) shop.php?id=$1 [L]
   RewriteRule ^shop/([0-9]+)/(.*)/ shop.php?id=$1 [L]

Hope this will work. Thanks.

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