htaccess 文件中重写规则的正确顺序
我需要:
http://www.example.com/v1/my-project/ 重定向到 http://example.com/my-project/
所以
:( 1)从http_host中删除www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
(2)删除request_uri的'v1/'部分
RewriteCond %{REQUEST_URI} ^/v1/(.*)$ [NC]
RewriteRule . %1 [R=301,L]
(3)我还想将所有404重定向到主页。
ErrorDocument 404 /
(4) 最后,我的所有文档实际上都驻留在托管当前活动网站的“v2/”文件夹中,但我不希望网址中包含“v2”,而只需要“/”,
RewriteCond %{REQUEST_URI} !^/v2/ [NC]
RewriteRule ^(.*)$ /v2/$1 [NC,L]
所以,这是我的规则。 我的问题是:我不管理(2):它被重定向到/(因为我猜是规则(3)。我认为我的规则的顺序一定是错误的,但我似乎无法正确理解。可以你帮忙 ?
I need to have :
http://www.example.com/v1/my-project/ redirected to http://example.com/my-project/
so :
(1) remove the www from the http_host
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
(2) remove the 'v1/' part of the request_uri
RewriteCond %{REQUEST_URI} ^/v1/(.*)$ [NC]
RewriteRule . %1 [R=301,L]
(3) I also want to redirect all 404 to the homepage.
ErrorDocument 404 /
(4) Finally, all my documents actually reside in a "v2/" folder which hosts the current active website, but i don't want "v2" in the url, just "/"
RewriteCond %{REQUEST_URI} !^/v2/ [NC]
RewriteRule ^(.*)$ /v2/$1 [NC,L]
So, here are my rules. My question is: i don't manage (2): it gets redirected to / (because of rule (3) i guess. I think the order of my rules must be faulty but i can't seem to get it right. Can you help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“规则 3”根本不是规则,它相对于 RewriteRules 的顺序并不重要。 规则 2 由于某些其他原因而失败。 我不确定它是否能解决您的问题,但我会通过这样编写来简化您的规则:
"Rule 3" isn't a rule at all, and its order relative to your RewriteRules doesn't matter. Rule 2 is failing for some other reason. I'm not sure whether it will address your problem, but I would simplify your rules somewhat by writing them like this:
您应该首先编写导致外部重定向(
R
标志)的任何规则,然后编写其他规则。 否则,已经重写的 URL 可以用于外部重定向,尽管它只是用于内部重定向。所以我不会改变你现在的订单。
You should first write any rule that is causing an external redirect (
R
flag) and then the other rules. Otherwise an already rewritten URL can be used for an external redirect though it was just intended for an internal redirect.So I won’t change the order you have right now.