mod_rewrite:重定向到与域同名的文件夹

发布于 2024-10-04 20:11:38 字数 358 浏览 1 评论 0原文

我有一堆域指向同一个文件夹,即我的主机“public_html”的根目录。 我希望我可以重定向它们,每个重定向到一个与域同名的文件夹。

例如:将 www.mydomain.com 重定向到“public_html/www.mydomain.com”

我尝试了以下操作:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/.*$
RewriteRule ^(.*)$  /%{HTTP_HOST}/$1 [L]

但没有成功。怎么了? 甚至可以重定向到名称上使用“点”的文件夹吗?

提前致谢。

I have a bunch of domains pointing to the same folder, the root of my host "public_html".
I wish I could redirect them, each one for a folder with the same name as the domain.

eg: redirect www.mydomain.com to "public_html/www.mydomain.com"

I tried this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/.*$
RewriteRule ^(.*)$  /%{HTTP_HOST}/$1 [L]

But with no success. What's wrong?
Its even possible to redirect to folder that uses a 'dot' on its name?

Thanks in advanced.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不必了 2024-10-11 20:11:38

RewriteCond 中的测试模式实际上是尝试匹配以文字字符串 /%{HTTP_HOST}/ 开头的输入。 %{HTTP_HOST} 变量的值不会像您期望的那样扩展,因此条件将始终为 true(模式本身永远不会匹配)。您需要修改 RewriteCond,有几种不同的方法。

如果这是您执行的唯一重写,您可以检查是否已经完成:

RewriteCond %{ENV:REDIRECT_STATUS} =""

或者,如果在重写之前资源不存在,您可以检查它:

RewriteCond %{REQUEST_FILENAME} !-f

最后,您可以模仿您的通过在测试模式中使用反向引用以及不会出现在 URL 中的分隔符来恢复原始条件:

RewriteCond /%{HTTP_HOST}/#%{REQUEST_URI} !^([^#]+)#\1

请注意,这并不能保证您已完成重定向,它仅确保请求 URI 具有主机名称作为其第一个路径段。不过,这通常已经足够好了。

The test pattern in your RewriteCond is actually trying to match input starting with the literal string /%{HTTP_HOST}/. The value of the %{HTTP_HOST} variable is not expanded like you were expecting, so the condition will always be true (the pattern itself will never match). You'll need to modify the RewriteCond, and there are a few different approaches.

If this is the only rewrite you perform, you can just check to see if you've done it yet:

RewriteCond %{ENV:REDIRECT_STATUS} =""

Or, if the resource won't exist until you rewrite it, you can check for that instead:

RewriteCond %{REQUEST_FILENAME} !-f

Finally, you can mimic your original condition by using backreferences within the test pattern, along with a separator character that won't appear in your URL:

RewriteCond /%{HTTP_HOST}/#%{REQUEST_URI} !^([^#]+)#\1

Note that this doesn't guarantee that you've done the redirection though, it only ensures that the request URI has the host name as its first path segment. However, that's generally good enough.

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