繁体   English   中英

php htaccess RewriteRule如何修复

[英]Php htaccess RewriteRule how to fix

我从url中获得$ _GET ['id']来显示页面,到htaccess,我有以下代码显示为shop/123而不是shop.php?id=123

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

我的问题是..如果我添加到URL shop/123/test/test/test这仍然可以正常工作,并且我在共享链接等方面有问题。

我如何解决它只需花一个参数即可进入404?

对不起我的英语不好

您可以使用:

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

这会将/ shop / foobar重写为/shop.php?id=foobar,但不会使用斜杠或多余的path-info重写/ shop / foobar /。

如果id仅是数字,则可以将重写规则更改为此:

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

如果您在id也有字母:

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

尝试这个:

    <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]

希望这会起作用。 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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