mod URL 重写无效

mod URL rewrite is not working

http://www.example.com/index.php?page=property_submit&edit=1&pid=978

我想将这个 url 重写为

http://www.example.com/index/property_submit/1/978

我尝试使用以下代码重写,但它不起作用:

RewriteRule ^index/([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?=

RewriteRule ^index/([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?= [QSA]

完整代码如下:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) example.com/ [R=301,L]
RewriteRule ^index/([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?=
RewriteRule ^index/([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?= [QSA]

您的模式与您要重写的 url 不匹配。

改为考虑这个:

RewriteEngine on
RewriteRule ^index/property_submit/([0-9]+)/([0-9]+)$ index.php?page=property_submit&edit=&pid= [L]

我认为匹配 'property_submit' 字符串可以让你的策略更安全,因为你不匹配任意 urls 你可能 不想重写.

您必须处理原始请求以处理重定向:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^GET\ /index\.php\?page=([^\s&]+)&edit=(\d+)&pid=(\d+) [NC]
RewriteRule ^ /index/%1/%2/%3? [R=301,L,NC]

RewriteRule ^index/([\w-]+)/(\d+)/(\d+)$ /index.php?page=&edit=&pid= [L]

根据您的要求,

http://www.example.com/index.php?page=property_submit&edit=1&pid=978

我想将这个url重写为

http://www.example.com/index/property_submit/1/978

试试下面的代码,

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) example.com/ [R=301,L]
RewriteRule ^index/([a-zA-Z0-9_-]+)/([0-9]+)/([0-9]+)$ index.php?page=&edit=&pid=