Mafiozaros3
Mafiozaros3

Reputation: 31

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

Upvotes: 2

Views: 129

Answers (3)

Amit Verma
Amit Verma

Reputation: 41219

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 .

Upvotes: 3

Kalyan Halder
Kalyan Halder

Reputation: 1585

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.

Upvotes: 0

Koala Yeung
Koala Yeung

Reputation: 7863

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

Upvotes: 1

Related Questions