简体   繁体   English

.htaccess重写多个参数的规则

[英].htaccess rewrite rules for multiple parameters

Rewriting the following 3 URLs 重写以下3个网址

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

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

http://example.com/index.php?page=stats&type=daily to http://example.com/stats/daily http://example.com/index.php?page=stats&type=daily to http://example.com/stats/daily

Current .htaccess is written as the following: 当前的.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]

I need to remove /index/ from the first and third URL, and /index/profile/ from the second one. 我需要从第一个和第三个URL中删除/ index /,从第二个URL中删除/ index / profile /。

All comments & suggestions are welcomed. 欢迎提出所有意见和建议。

Presumably these are 3 exceptions (special cases) to the current rules, so the existing rules should not change. 据推测,这些是当前规则的3个例外 (特殊情况),因此现有规则不应改变。 (?) (?)

Add the following "special cases" immediately after your RewriteBase directive, before your existing rules: 在现有规则之前,在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]

If you then need to internally rewrite these specific URLs back into the "real" parameterized versions then, at the end of your .htaccess file: 如果您需要在内部将这些特定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]

(I've removed the NC flag - add this back if you really need it.) (我已经删除了NC标志 - 如果你确实需要它,请添加它。)

You can't have both the search and profile be the same regex pattern. 您不能让搜索和配置文件都是相同的正则表达式模式。 For example, if someone requests: 例如,如果有人请求:

http://example.com/index/foo

Are they trying to go to a page called "foo" or go to a profile of a user named "foo"? 他们是否试图访问名为“foo”的页面或转到名为“foo”的用户的个人资料? You need to add something that separates the two, maybe something like: 你需要添加一些分隔两者的东西,可能是这样的:

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]

This makes your urls look like: 这使您的网址看起来像:

http://example.com/profile/myname

http://example.com/page/search

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

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

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