htaccess 文件中重写规则的正确顺序

发布于 2024-07-27 05:29:37 字数 877 浏览 6 评论 0原文

我需要:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢望 2024-08-03 05:29:37

“规则 3”根本不是规则,它相对于 RewriteRules 的顺序并不重要。 规则 2 由于某些其他原因而失败。 我不确定它是否能解决您的问题,但我会通过这样编写来简化您的规则:

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

RewriteRule ^v1/(.*) /$1 [R=301,L,NC]

RewriteCond %{REQUEST_URI} !^/v2/ [NC]
RewriteRule (.*) /v2/$1 [NC,L]

"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:

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

RewriteRule ^v1/(.*) /$1 [R=301,L,NC]

RewriteCond %{REQUEST_URI} !^/v2/ [NC]
RewriteRule (.*) /v2/$1 [NC,L]
东北女汉子 2024-08-03 05:29:37

您应该首先编写导致外部重定向(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文