【发布时间】:2023-04-02 21:35:01
【问题描述】:
我要实现以下目标
- 像 mydomain.com/anyword 这样的任何链接都应该重写为 index.php?pg=$1
示例:
mydomain.com/new -->index.php?pg=new
mydomain.com/purchased -->index.php?pg=purchased
mydomain.com/login -->index.php?pg=login
- mydomain.com/anyword/anynumber 等任何链接都应重写为 index.php?pg=$1&id=$2
示例:
mydomain.com/new/12 -->index.php?pg=new&id=12
mydomain.com/purchased/240 -->index.php?pg=purchased&id=240
mydomain.com/login/10 -->index.php?pg=login&id=10
我使用了以下 htaccess 代码。 第一个工作正常。 关于第二个,整个参数传给pg,根本没有参数id。
这是在 url 的情况下 mydomain.com/purchased/240 在我的 php $_Get 中只有 'pg' 参数和 'id' 参数 和 pg=/purchased/240
请提出正确的方法。
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
RewriteRule ^([^/]+)/(\d+)/.*$ /index.php?pg=$1&id=$2 [NC,QSA]
谢谢
编辑:正确的解决方案,以防有人需要它
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+) /index.php?pg=$1&id=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
【问题讨论】:
-
你遇到了什么错误?
-
这个问题好像和HTA没什么关系?
-
@Teemu - 为什么不呢?重写规则有一些错误。我也尝试了其他规则。它不工作。
-
好的。自己找到的。必须为第二条规则再次重复重写条件。第二条规则应该是第一位的。编辑了答案。感谢@starkeen 提到应该提出第二条规则。
标签: php apache .htaccess mod-rewrite