.htaccess 子域重定向不起作用

发布于 2024-10-03 02:21:27 字数 596 浏览 3 评论 0原文

好吧,现在我迷路了。

我正在尝试将子域进行简单的 .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 技术交流群。

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

发布评论

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

评论(4

几味少女 2024-10-10 02:21:27

这只是一个“普通”重写(浏览器看不到它)。要重定向,请将 R 标志添加到您的 RewriteRule 中。

RewriteRule (.*) /s_%1/$1 [L,R]

其余的似乎是正确的,尽管我还没有测试过。对于调试,您可以考虑使用 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.

RewriteRule (.*) /s_%1/$1 [L,R]

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

追风人 2024-10-10 02:21:27

那么,这两种解决方案都不起作用吗?那么尝试一些简单的事情。

RewriteEngine on

RewriteCond ${SERVER_NAME} ^(subdomain)\.yoursite\.com$ [nc]
RewriteRule ^(.*)$ http://www.yoursite.com/s_%1/$1 [L,R]

要测试您的子域是否已正确处理,请创建 random.html 文件,将其放置在应读取的位置,然后尝试通过 http://subdomain.yoursite.com/random.html 打开它。然后您可以尝试一些类似的东西:

RewriteRule ^random.html - [F]

...如果这阻止了对文件的访问,请尝试

RewriteCond ${SERVER_NAME} ^subdomain\.yoursite\.com$ [nc]

在前面的规则之前添加,以阻止对该文件的访问,以确保重写引擎实际上符合您的规则。这将仅针对所需的子域(www.yoursite.com/random.html 应该有效,但通过子域访问不应该)。

然后,如果这些规则有效,只需添加更多内容并看看它何时停止工作即可。

So, neither solution does work? Try something simple then.

RewriteEngine on

RewriteCond ${SERVER_NAME} ^(subdomain)\.yoursite\.com$ [nc]
RewriteRule ^(.*)$ http://www.yoursite.com/s_%1/$1 [L,R]

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:

RewriteRule ^random.html - [F]

...and if that blocks access to file, try prepending

RewriteCond ${SERVER_NAME} ^subdomain\.yoursite\.com$ [nc]

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.

初懵 2024-10-10 02:21:27

RewriteRules 是个混蛋。

以下内容应该有效:

.htaccess

RewriteCond ${SERVER_NAME} !^(www|ftp|mail)\.example\.com$
RewriteCond ${SERVER_NAME} !^([^.]+)\.example\.com$
RewriteRule .* redirect.php?to=%1

redirect.php

<?php
   $desired_server_name = 'http://example.com';
   $subdir = 's_' . $_GET['to'];

   $url = $desired_server_name . '/' . $to . $_SERVER['REQUEST_URI'];

// Permanent redirects
   header('HTTP/1.1 301 Moved Permanently');

// Or simple redirects:
   header('HTTP/1.1 302 Found');

   header('Location: '.$url);
?>

在我的服务器上工作(debian 4/apache 2)。

额外奖励:永远不要使用 HTTP_HOST!请参阅以下请求:

 HTTP/1.1 GET /foo/bar.php
 Host: www.host.tld"><script>alert(/Hello/)</script
 Connection: close

如果您在 .php 脚本中使用 $_SERVER['HTTP_HOST'] 来构建相关链接或 .htaccess 规则,并且“www.host.tld”是虚拟主机或者为 Apache 配置的唯一主机,HTTP 请求标头中的 XSS 将不转义地传递下去。

RewriteRules are a bitch.

The following should work:

.htaccess:

RewriteCond ${SERVER_NAME} !^(www|ftp|mail)\.example\.com$
RewriteCond ${SERVER_NAME} !^([^.]+)\.example\.com$
RewriteRule .* redirect.php?to=%1

redirect.php

<?php
   $desired_server_name = 'http://example.com';
   $subdir = 's_' . $_GET['to'];

   $url = $desired_server_name . '/' . $to . $_SERVER['REQUEST_URI'];

// Permanent redirects
   header('HTTP/1.1 301 Moved Permanently');

// Or simple redirects:
   header('HTTP/1.1 302 Found');

   header('Location: '.$url);
?>

Works on my server (debian 4/apache 2).

Bonus: do not EVER use HTTP_HOST! See the following request:

 HTTP/1.1 GET /foo/bar.php
 Host: www.host.tld"><script>alert(/Hello/)</script
 Connection: close

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.

痴梦一场 2024-10-10 02:21:27

我们的虚拟机上也有类似的工作,我们将 everything.usertld 重定向到该域的文件夹,该文件夹位于 httpd.conf 中,在 .htaccess 中尝试过,但与您的一样,它不起作用。

调整它,这对我有用(我的虚拟机占用一个名为 benb 的 tld,但将其更改为您的域应该没问题):

    RewriteCond %{HTTP_HOST} !^www\.benb
    RewriteCond %{HTTP_HOST} ^(.*)\.benb
    RewriteCond %{REQUEST_URI} !^/{0,1}s_
    RewriteRule ^(.*)$ s_%1/$1 [L]

此外,这会捕获域之前的所有文本..您应该能够更改:

RewriteCond %{HTTP_HOST} ^(.*)\.benb

to

RewriteCond %{HTTP_HOST} ^([^.]+)\.benb

来处理仅 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):

    RewriteCond %{HTTP_HOST} !^www\.benb
    RewriteCond %{HTTP_HOST} ^(.*)\.benb
    RewriteCond %{REQUEST_URI} !^/{0,1}s_
    RewriteRule ^(.*)$ s_%1/$1 [L]

Also this captures all the text before the domain.. you should be able to change:

RewriteCond %{HTTP_HOST} ^(.*)\.benb

to

RewriteCond %{HTTP_HOST} ^([^.]+)\.benb

to handle just 1 level of subdomain. Also your other part about (www|ftp|mail) would work fine too.

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