.htaccess 子域到文件夹

发布于 2024-09-15 23:01:08 字数 1281 浏览 2 评论 0原文

我最近对网站的文件夹结构进行了一些细微的更改,现在我的重写规则之一似乎已损坏。

过去,我在 public_html 中有一个 /mydomain.com/ 文件夹,其中设置了 wiki。在同一文件夹中还有一些我用于子域访问的文件夹,例如成员和文件。

旧设置:

#www to main website
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteRule ^(.*)$ mydomain.com/$1 [L]

#subdomain to folder (members. => /members/, files. => /files/, etc)
RewriteCond %{HTTP_HOST} ^(.*).mydomain.com$
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*)$ mydomain.com/%1/$1 [L]

相当简单,当我输入 files.mydomain.com/myfile.zip 时,它可以正常工作,没有任何问题。

最近我安装了我的wiki的几种语言(实际上与问题无关,只是为了阐述情况)并且我制定了以下规则:

#to the right language folder (www = en)
RewriteCond %{HTTP_HOST} ^(www|nl|es).mydomain.com$
RewriteRule ^(.*)$ mydomain.com/%1/$1 [L]

#subdomain to folder (members. => /members/, files. => /files/, etc)
RewriteCond %{HTTP_HOST} ^(.*).mydomain.com$
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*)$ mydomain.com/misc/%1/$1 [L]

显然,不同语言的wiki设置在mydomain.com/www/,mydomain .com/es/ 等。这工作得很好。 问题出在第二部分,即子文件夹。在同一个 mydomain.com/ 文件夹中,我创建了一个 Misc/ 文件夹来存储所有杂项内容(包括子域文件夹)。我认为只需在路径中添加 /misc/ (就像我在第一条规则中添加语言文件夹名称一样)即可使其工作..但它给了我一个 500 错误。 旧设置和新设置在任何文件夹中都没有任何可能与第二条规则冲突的 .htaccess 行。

任何人都可以发现错误,或者告诉我如何系统地检查此设置是否存在错误?

I've recently made some minor changes to my website's folder structure, and now one of my rewriterules seems broken.

in the past I had a /mydomain.com/ folder in public_html in which a wiki was set up. In the same folder were some folders that I used for subdomain access, like members and files.

The old setup:

#www to main website
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteRule ^(.*)$ mydomain.com/$1 [L]

#subdomain to folder (members. => /members/, files. => /files/, etc)
RewriteCond %{HTTP_HOST} ^(.*).mydomain.com$
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*)$ mydomain.com/%1/$1 [L]

Fairly easy, and when I typed in files.mydomain.com/myfile.zip it worked without any problems.

Recently I installed several languages of my wiki (which is in fact irrelevant to the question, but just to elaborate the situation) and I made the following rule:

#to the right language folder (www = en)
RewriteCond %{HTTP_HOST} ^(www|nl|es).mydomain.com$
RewriteRule ^(.*)$ mydomain.com/%1/$1 [L]

#subdomain to folder (members. => /members/, files. => /files/, etc)
RewriteCond %{HTTP_HOST} ^(.*).mydomain.com$
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*)$ mydomain.com/misc/%1/$1 [L]

Obviously, the different language wikis are set up in mydomain.com/www/, mydomain.com/es/, etc. This works perfectly fine.
The problem lies in the second part, the subfolders. In the same mydomain.com/ folder I created a misc/ folder to store all the misc stuff (including the subdomain folders). I figured just adding the /misc/ in the path (like I added the language folder name in the first rule) would make it work.. but it gives me a 500 error.
Neither the old setup nor the new setup have any .htaccess lines in any folders that could conflict with the second rule.

Can anyone spot the error, or tell me how to systematically check this setup for bugs?

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

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

发布评论

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

评论(1

千秋岁 2024-09-22 23:01:08

我认为防止发生重定向循环的最简单方法是检查您是否已将 URL 重写到您想要的位置。我们可以通过几种不同的方式来做到这一点;例如,如果您知道重写后该文件将存在,则可以以 %{REQUEST_FILENAME} !-f 为条件。

就您而言,由于您要将所有内容重写到 /public_html 目录中的公共文件夹中,因此我们可以检查是否已完成:

#to the right language folder (www = en)
RewriteCond %{HTTP_HOST} ^(www|nl|es)\.example\.com$
RewriteCond %{REQUEST_URI} !^/example\.com
RewriteRule ^(.*)$ example.com/%1/$1 [L]

#subdomain to folder (members. => /members/, files. => /files/, etc)
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{REQUEST_URI} !^/example\.com
RewriteRule ^(.*)$ example.com/misc/%1/$1 [L]

诚然,我不确定您为何遇到问题现在,但以前不是。我在测试服务器上运行了您的原始规则集,但由于内部重定向太多而最终出现内部服务器错误,因此我们的设置一定存在差异。但无论如何,希望这能让你一切顺利。

The easiest way to prevent the redirection loop I believe is happening is to just check if you've already rewritten the URL to where you intended it to go. We can do this a few different ways; if you know that the file will exist once you've rewritten it, you can condition on %{REQUEST_FILENAME} !-f, for instance.

In your case, since you're rewriting everything to a common folder within your /public_html directory, we can just check if that's already been done:

#to the right language folder (www = en)
RewriteCond %{HTTP_HOST} ^(www|nl|es)\.example\.com$
RewriteCond %{REQUEST_URI} !^/example\.com
RewriteRule ^(.*)$ example.com/%1/$1 [L]

#subdomain to folder (members. => /members/, files. => /files/, etc)
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{REQUEST_URI} !^/example\.com
RewriteRule ^(.*)$ example.com/misc/%1/$1 [L]

Admittedly, I'm not sure why you're experiencing issues now, but weren't before. I ran your original rule set on my test server and ended up with an internal server error due to too many internal redirections, so there must be differences in our setup. In any case though, hopefully this will get things working for you.

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