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

时间:2021-08-24 11:20:13

I need to have :

我需要:

http://www.example.com/v1/my-project/ redirected to http://example.com/my-project/

http://www.example.com/v1/my-project/重定向到http://example.com/my-project/

so :

(1) remove the www from the http_host

(1)从http_host中删除www

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

(2) remove the 'v1/' part of the request_uri

(2)删除request_uri的'v1 /'部分

RewriteCond %{REQUEST_URI} ^/v1/(.*)$ [NC]
RewriteRule . %1 [R=301,L]

(3) I also want to redirect all 404 to the homepage.

(3)我还想将所有404重定向到主页。

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

(4)最后,我的所有文档实际上都位于一个“v2 /”文件夹中,该文件夹托管当前活动的网站,但我不想在网址中使用“v2”,只是“/”

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 ?

所以,这是我的规则。我的问题是:我没有管理(2):它被重定向到/(因为规则(3)我猜。我认为我的规则的顺序必定是错误的但我似乎无法做到正确。可以你帮忙 ?

2 个解决方案

#1


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

“规则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]

#2


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.

您应该首先编写导致外部重定向(R标志)的任何规则,然后编写其他规则。否则,已经重写的URL可用于外部重定向,尽管它仅用于内部重定向。

So I won’t change the order you have right now.

所以我不会改变你现在的订单。

#1


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

“规则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]

#2


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.

您应该首先编写导致外部重定向(R标志)的任何规则,然后编写其他规则。否则,已经重写的URL可用于外部重定向,尽管它仅用于内部重定向。

So I won’t change the order you have right now.

所以我不会改变你现在的订单。