.htaccess 子域重定向不起作用
好吧,现在我迷路了。
我正在尝试将子域进行简单的 .htaccess 重定向到服务器上的特定文件夹,这意味着所有子域
subdomain.mywebsite.com
都会转到
www.mywebsite.com/s_subdomain
但由于某些原因这不起作用。
我在 .htaccess 中尝试了很多设置,但没有效果。现在,在我的 .htaccess 中,我有:
RewriteEngine on
Options +FollowSymLinks
Options +SymlinksIfOwnerMatch
RewriteCond %{HTTP_HOST} !^(www|ftp|mail)\.mywebsite\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mywebsite\.com
RewriteRule (.*) /s_%1/$1 [L]
是否还有其他设置,或者我错过了什么?
附言。 - 我无法访问 http.conf。我必须只使用 .htaccess
谢谢!
Ok, now I am lost.
I am trying to do a simple .htaccess redirect of subdomains to a specific folder on the server, meaning all
subdomain.mywebsite.com
will go to
www.mywebsite.com/s_subdomain
But for some reasons this doesn't work.
I have tried a lot of settings in .htaccess but for no good. Now in my .htaccess I have:
RewriteEngine on
Options +FollowSymLinks
Options +SymlinksIfOwnerMatch
RewriteCond %{HTTP_HOST} !^(www|ftp|mail)\.mywebsite\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mywebsite\.com
RewriteRule (.*) /s_%1/$1 [L]
Are there any other settings, or is somethig I have missed?
PS. - I don't have access to http.conf. I have to do it using only .htaccess
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这只是一个“普通”重写(浏览器看不到它)。要重定向,请将 R 标志添加到您的 RewriteRule 中。
其余的似乎是正确的,尽管我还没有测试过。对于调试,您可以考虑使用 RewriteLog,请参阅 http://httpd.apache.org /docs/2.0/mod/mod_rewrite.html#rewritelog
This is just a "plain" rewrite (the browser won't see it). To redirect, add the R flag to your RewriteRule.
The rest seems right, although I haven't tested it. For debugging you could consider RewriteLog, see http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritelog
那么,这两种解决方案都不起作用吗?那么尝试一些简单的事情。
要测试您的子域是否已正确处理,请创建 random.html 文件,将其放置在应读取的位置,然后尝试通过
http://subdomain.yoursite.com/random.html
打开它。然后您可以尝试一些类似的东西:...如果这阻止了对文件的访问,请尝试
在前面的规则之前添加,以阻止对该文件的访问,以确保重写引擎实际上符合您的规则。这将仅针对所需的子域(www.yoursite.com/random.html 应该有效,但通过子域访问不应该)。
然后,如果这些规则有效,只需添加更多内容并看看它何时停止工作即可。
So, neither solution does work? Try something simple then.
To test if your subdomain is handled correctly, create random.html file, place it where it should be read from, and try opening it via
http://subdomain.yoursite.com/random.html
. Then you can try some stuff like:...and if that blocks access to file, try prepending
to previous rule, to block access to that file, to make sure that rewrite engine is actually hitting your rules. That would target only desired subdomain (www.yoursite.com/random.html should work, but access via subdomain shouldn't).
Then if those rules work, it's just a matter of adding more stuff and see when it stops working.
RewriteRules 是个混蛋。
以下内容应该有效:
.htaccess:
redirect.php
在我的服务器上工作(debian 4/apache 2)。
额外奖励:永远不要使用 HTTP_HOST!请参阅以下请求:
如果您在 .php 脚本中使用
$_SERVER['HTTP_HOST']
来构建相关链接或 .htaccess 规则,并且“www.host.tld”是虚拟主机或者为 Apache 配置的唯一主机,HTTP 请求标头中的 XSS 将不转义地传递下去。RewriteRules are a bitch.
The following should work:
.htaccess:
redirect.php
Works on my server (debian 4/apache 2).
Bonus: do not EVER use HTTP_HOST! See the following request:
If you use
$_SERVER['HTTP_HOST']
in your .php scripts to construct links or .htaccess rules for that matter and "www.host.tld" is the virtual-host or the only host configured for Apache, the XSS in the HTTP request header will be passed down unescaped.我们的虚拟机上也有类似的工作,我们将 everything.usertld 重定向到该域的文件夹,该文件夹位于 httpd.conf 中,在 .htaccess 中尝试过,但与您的一样,它不起作用。
调整它,这对我有用(我的虚拟机占用一个名为 benb 的 tld,但将其更改为您的域应该没问题):
此外,这会捕获域之前的所有文本..您应该能够更改:
to
来处理仅 1子域级别。另外,您关于 (www|ftp|mail) 的其他部分也可以正常工作。
We have a similar thing working on our Virtual Machines, where we redirect anything.usertld to a folder for that domain, that was in httpd.conf, tried in in the .htaccess and like yours it didn't work.
Tweaking it, this works for me (my VM occupies a tld called benb, but changing it to your domain should be fine):
Also this captures all the text before the domain.. you should be able to change:
to
to handle just 1 level of subdomain. Also your other part about (www|ftp|mail) would work fine too.