在 htaccess 文件中创建 mod_rewrite 规则
我正在尝试创建一个 mod_rewrite
规则来将人们定向到子文件夹。当前代码如下所示:
RewriteEngine On
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
如果用户键入 www.abcsite.com
,则重定向有效,但如果用户键入 abc.com
,则重定向无效。我是否遗漏了某些内容或应该采取不同的措施来确保用户转到正确的文件夹(无论他们如何键入 URL)?
旁注:我正在处理的 htaccess
文件是一个 Joomla 文件,因此它的所有内容都涉及另一个 Joomla 站点。我很感激你的帮助。
I am trying to create a mod_rewrite
rule to direct people to a sub-folder. Currently the code looks as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
The redirect works if the user types www.abcsite.com
, but not if they type abc.com
. Is there something that I am missing or should do differently to make sure the user goes to the correct folder (regardless of how they type the URL)?
Side note: The htaccess
file that I am dealing with is a Joomla file, so all contents of it deal with another Joomla site. I appreciate the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为你有条件。
所有上述规则只会通过其 abcsite.com
您还添加以下规则,那么它也适用于 abc.com。
Because you have conditions for that.
All above rules will pass only its abcsite.com
You add following rules also then it work for abc.com too.
您的第二个条件中有一个杂散的
!
。模式前面的!
表示当正则表达式不匹配时条件为真(如第三个条件)。模式内的!
只是一个文字符号。主机条件应该类似于:
事实上,它们可以连接成一个条件(注意,这里没有
[OR]
):第三个条件旨在防止重定向循环(
/foo
→/abc/foo
→/abc/abc/foo
→ …)。它的意思是,如果请求 URL 以/abc/
开头,则不应用该规则。但是,您的实际重定向是内部重定向:如果用户访问abcsite.com/foo
,服务器会在内部将其重写为/webroot/abc/foo
,但是REQUEST_URI
保持不变,/foo
。这不会导致重定向循环的原因很可能是在
abc/.htaccess
中重写规则,一旦重定向完成,这些规则就会覆盖此规则。第三个条件应该检查的是重写规则匹配的路径:
There's a stray
!
in your second condition. A!
in front of the pattern means that the condition is true when the regex doesn't match (like in the third condition). A!
inside the pattern is just a literal symbol.The host conditions should be something like:
And in fact, they can be joined into a single condition (note, no
[OR]
here):Your third condition is intended to prevent redirect loops (
/foo
→/abc/foo
→/abc/abc/foo
→ …). What it says is that the rule isn't applied if the request URL starts with/abc/
. However, your actual redirect is an internal redirect: if a user accessesabcsite.com/foo
, the server internally rewrites this to/webroot/abc/foo
, butREQUEST_URI
stays the same,/foo
.The reason this doesn't cause a redirect loop as it is is likely rewrite rules in
abc/.htaccess
which override this one once the redirect is done.What should be checked instead in the third condition is the path matched by the rewrite rule: