重写规则的htaccess文件正确的顺序 [英] right order of rewrite rules in an htaccess file

查看:148
本文介绍了重写规则的htaccess文件正确的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有:

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

这样:

(1)从HTTP_HOST

(1) remove the www from the http_host

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

(2)删除了V1 /'的REQUEST_URI的一部分

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

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

(3)我也想重定向所有404主页。

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

ErrorDocument 404 /

(4)最后,我所有的文件实际上存在于它承载当前活动的网站V2 /文件夹中,但我不希望在URLV2,只是/

(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]

所以,这里是我的规则。我的问题是:我不管理(2):它被重定向(规则来/原因(3)我想我觉得我的规则的顺序一定是错误的,但我似乎无法得到它的权利能。你帮忙吗?

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 ?

推荐答案

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

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]

这篇关于重写规则的htaccess文件正确的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆