Php .htaccess多个参数的重写规则

Php .htaccess多个参数的重写规则,php,.htaccess,url-rewriting,rewrite,url-routing,Php,.htaccess,Url Rewriting,Rewrite,Url Routing,重写以下3个URL http://example.com/index.php?page=search 到 http://example.com/search http://example.com/index.php?page=profile&value=myname 到 http://example.com/myname http://example.com/index.php?page=stats&type=daily 到 http://example.com/stats/daily 当前.h

重写以下3个URL

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

http://example.com/index.php?page=profile&value=myname
http://example.com/myname

http://example.com/index.php?page=stats&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
标志-如果确实需要,请将其添加回去。)

您不能让搜索和配置文件都是相同的正则表达式模式。例如,如果有人请求:

http://example.com/index/foo
他们是想转到一个名为“foo”的页面还是一个名为“foo”的用户的个人资料?您需要添加一些将两者分开的内容,例如:

Options -MultiViews
RewriteEngine On
RewriteBase /

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

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

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

RewriteRule ^profile/([^/]+)/?$ /index.php?page=profile&value=$1 [NC,L,QSA]
RewriteRule ^page/([^/]+)/([^/]+)/?$ /index.php?page=$1&type=$2 [NC,L,QSA]
RewriteRule ^page/([^/]+)/?$ /index.php?page=$1 [NC,L,QSA]
这使您的URL看起来像:

http://example.com/profile/myname

http://example.com/page/search

http://example.com/page/stats/daily

您当前的.htaccess规则存在问题,因为您正在将
page=X&value=Y
page=X&type=Y
重定向到同一URL,即
/index/X/Y
-这意味着您无法将其重写回来。您的
重写规则中有两个具有相同的模式,因此第二个将永远不匹配。它将始终重写回
page=X&value=Y
类型
丢失)。感谢您的反馈。我试图从url中删除第一个查询,并重写如下:是的,这就是我认为上述指令应该实现的,对于您问题中所述的3个特定url(尽管您上面评论中所述的url不同?)。但是,如果这是一个“通用”重写,例如
example.com/profile/
,那么这将无法按预期工作,因为它引入了歧义(一对多的关系,无法转换回)。但是上面建议的重写规则似乎不起作用。当应用这些建议的重写规则时,最初列出的URL将不会打开。您所说的“似乎不起作用”是什么意思?它有什么作用吗?这些指令应该在给定的上下文中工作(我也在自己的服务器上测试了它们以确保)。这些指令的顺序很重要(如上所述)。您的.htaccess中的其他指令可能会冲突吗?除了问题中所述的指令之外,您的.htaccess文件中还有其他内容吗?好的。尝试清理现有的规则,只使用你的-它确实有效!谢谢你的反馈。我试图从url中删除第一个查询,并重写如下:to to to