.htaccess多个参数的重写规则 [英] .htaccess rewrite rules for multiple parameters
问题描述
重写以下3个URL
http://example.com/index.php?page=search
到
http://example.com/search
http://example.com/index.php?page=profile&value=我的名字
到
http:// example。 com / myname
http://example.com/index.php?page=stats&type=daily
到
http://example.com/stats/daily
当前.htaccess的写法如下:
Current .htaccess is written as the following:
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/。
I need to remove /index/ from the first and third URL, and /index/profile/ from the second one.
所有评论&欢迎提出建议。
All comments & suggestions are welcomed.
推荐答案
搜索和配置文件不能同时使用相同的正则表达式模式。例如,如果有人请求:
You can't have both the search and profile be the same regex pattern. For example, if someone requests:
http://example.com/index/foo
他们是要转到名为 foo的页面还是转到名为 foo的用户的个人资料?您需要添加将两者分开的内容,例如:
Are they trying to go to a page called "foo" or go to a profile of a user named "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
这篇关于.htaccess多个参数的重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!