php-.htaccess的多个参数重写规则

重写以下3个URL

http://example.com/index.php?page=search

http://example.com/search

http://example.com/index.php?page=profile\u0026amp;value=我的名字

http://example.com/myname

http://example.com/index.php?page=stats\u0026amp;type=daily

http://example.com/stats/daily

当前的.htaccess编写如下:

Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&value=([^\s&]+) [NC]
RewriteRule ^ index/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&type=([^\s&]+) [NC]
RewriteRule ^ index/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ index/%1/? [R=302,L,NE]

RewriteRule ^index/([^/]+)/([^/]+)/?$/index.php?page=$1&value=$2 [NC,L,QSA]
RewriteRule ^index/([^/]+)/([^/]+)/?$/index.php?page=$1&type=$2 [NC,L,QSA]
RewriteRule ^index/([^/]+)/?$/index.php?page=$1 [NC,L,QSA]

我需要从第一个和第三个URL中删除/ index /,并从第二个URL中删除/ index / profile /.

所有评论和欢迎提出建议.

解决方法:

大概这是当前规则的3个例外(特殊情况),因此现有规则不应更改. (?)

在您的RewriteBase指令之后,现有规则之前立即添加以下“特殊情况”:

RewriteCond %{THE_REQUEST} /index\.php\?page=(search)
RewriteRule ^ /%1? [R=302,L]

RewriteCond %{THE_REQUEST} /index\.php\?page=profile&value=(myname)
RewriteRule ^ /%1? [R=302,L]

RewriteCond %{THE_REQUEST} /index\.php\?page=(stats)&type=(daily)
RewriteRule ^ /%1/%2? [R=302,L]

如果随后需要在内部将这些特定的URL改写回“真实的”参数化版本,则在.htaccess文件的末尾:

RewriteRule ^(search)$/index.php?page=$1 [L,QSA]
RewriteRule ^(myname)$/index.php?page=profile&value=$1 [L,QSA]
RewriteRule ^(stats)/(daily)$/index.php?page=$1&type=$2 [L,QSA]

(我已删除了NC标志-如果您确实需要,请重新添加.)

上一篇:Nginx-Location匹配与Rewrite重写跳转


下一篇:php-Apache重写规则混乱