在 htaccess 文件中创建 mod_rewrite 规则

发布于 2024-10-18 22:13:37 字数 496 浏览 3 评论 0原文

我正在尝试创建一个 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 技术交流群。

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

发布评论

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

评论(2

迷鸟归林 2024-10-25 22:13:37

因为你有条件。

RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$ 
RewriteCond %{REQUEST_URI} !^/abc/.*$ 

所有上述规则只会通过其 abcsite.com

您还添加以下规则,那么它也适用于 abc.com。

RewriteCond %{HTTP_HOST} abc.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abc\.*$ 
RewriteCond %{REQUEST_URI} !^/abc/.*$ 
RewriteRule (.*)$ /abc/$1 [L]

Because you have conditions for that.

RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$ 
RewriteCond %{REQUEST_URI} !^/abc/.*$ 

All above rules will pass only its abcsite.com

You add following rules also then it work for abc.com too.

RewriteCond %{HTTP_HOST} abc.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abc\.*$ 
RewriteCond %{REQUEST_URI} !^/abc/.*$ 
RewriteRule (.*)$ /abc/$1 [L]
心安伴我暖 2024-10-25 22:13:37

您的第二个条件中有一个杂散的 ! 。模式前面的 ! 表示当正则表达式匹配时条件为真(如第三个条件)。模式内的 ! 只是一个文字符号。

主机条件应该类似于:

RewriteCond %{HTTP_HOST} ^abcsite\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.abcsite\.com$ [NC]

事实上,它们可以连接成一个条件(注意,这里没有 [OR]):

RewriteCond %{HTTP_HOST} ^(www\.)?abcsite\.com$ [NC]

第三个条件旨在防止重定向循环(/foo/abc/foo/abc/abc/foo → …)。它的意思是,如果请求 URL 以 /abc/ 开头,则不应用该规则。但是,您的实际重定向是内部重定向:如果用户访问 abcsite.com/foo,服务器会在内部将其重写为 /webroot/abc/foo,但是 REQUEST_URI 保持不变,/foo

这不会导致重定向循环的原因很可能是在 abc/.htaccess 中重写规则,一旦重定向完成,这些规则就会覆盖此规则。

第三个条件应该检查​​的是重写规则匹配的路径:

RewriteCond $1 !^abc/
RewriteRule (.*) /abc/$1 [L]

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:

RewriteCond %{HTTP_HOST} ^abcsite\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.abcsite\.com$ [NC]

And in fact, they can be joined into a single condition (note, no [OR] here):

RewriteCond %{HTTP_HOST} ^(www\.)?abcsite\.com$ [NC]

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 accesses abcsite.com/foo, the server internally rewrites this to /webroot/abc/foo, but REQUEST_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:

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