将 www.example.com/folder 重定向到 folder.example.com

将 www.example.com/folder 重定向到 folder.example.com

我将 mantis 设置为 www.example.com/mantis ,现在我已设置 httpd.conf 以便可以通过 mantis.example.com 访问。由于我有许多链接引用了之前的链接,因此我需要将它们重定向到新位置。

这是我现在的 httpd.conf:

<VirtualHost MYSERVERIP:80>
    ServerAlias EXAMPLE.com
    DocumentRoot /var/www/html/beta
    ServerName EXAMPLE.com
    UseCanonicalName On

 <IfModule mod_rewrite.c>
    RewriteEngine On

RewriteLogLevel 5
RewriteLog "/var/www/html/htaccess.log"

RewriteRule ^/mantis/(.*) http://mantis.EXAMPLE.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
...
 </IfModule>
</VirtualHost>

...

<VirtualHost MYSERVERIP:80>
    ServerAlias mantis.EXAMPLE.com
    DocumentRoot /var/www/html/mantis
    ServerName mantis.EXAMPLE.com
    UseCanonicalName On
</VirtualHost>

然而,访问http://www.EXAMPLE.com/mantis/view.php?id=17575导致:

http://guide.opendns.com/main?url=www.mantis.EXAMPLE.com%2Fview.php%3Fid%3D17575 

这表明:

You tried to visit www.mantis.EXAMPLE.com, which is not loading.

注意 www. 部分。我不明白它来自哪里!!

htaccess日志如下:

MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (2) init rewrite engine with requested uri /mantis/view.php
MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (3) applying pattern '^/mantis/(.*)' to uri '/mantis/view.php'
MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (2) rewrite '/mantis/view.php' -> 'http://mantis.EXAMPLE.com/view.php'
MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (2) explicitly forcing redirect with http://mantis.EXAMPLE.com/view.php
MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (1) escaping http://mantis.EXAMPLE.com/view.php for redirect
MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (1) redirect to http://mantis.EXAMPLE.com/view.php?id=17575 [REDIRECT/301]

有关的:httpd.conf 更改为允许 www.mysite.com 和 www.mysite.com/mantis 和 phpmyadmin?


编辑1

将 httpd.conf 更改为:

RewriteLogLevel 5
RewriteLog "/var/www/html/htaccess.log"

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^/mantis/(.*) http://mantis.EXAMPLE.com/$1 [L,R=301]

还是没有运气。

日志文件如下:

MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (2) init rewrite engine with requested uri /mantis/view.php
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (3) applying pattern '^' to uri '/mantis/view.php'
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (4) RewriteCond: input='www.EXAMPLE.com' pattern='!^www\.' => not-matched
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (3) applying pattern '^/mantis/(.*)' to uri '/mantis/view.php'
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (2) rewrite '/mantis/view.php' -> 'http://mantis.EXAMPLE.com/view.php'
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (2) explicitly forcing redirect with http://mantis.EXAMPLE.com/view.php
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (1) escaping http://mantis.EXAMPLE.com/view.php for redirect
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (1) redirect to http://mantis.EXAMPLE.com/view.php?id=17575 [REDIRECT/301]

答案1

唉……找到罪魁祸首了……

/mantis/ 中有一个 .htaccess,它把事情搞乱了... :(

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

刚刚将其删除并且现在一切正常!

答案2

那里有NameVirtualHost MYSERVERIP指令吗?如果没有,我确信将那一行添加到顶部可以解决您的问题。如果没有它,apache 只会忽略主机标头并使用它根据 IP 找到的第一个匹配的虚拟主机,这就是为什么它从第一个 vhost 重写规则重新添加 www 的原因。

答案3

好吧,检查一下这里的块:

RewriteRule ^/mantis/(.*) http://mantis.EXAMPLE.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

第一行是正确的。

第二个获取 HTTP_HOST,现在是 mantis.EXAMPLE.com。

然后第四行将其重写为 www.mantis.EXAMPLE.com

最简单的解决方法是将第一行移到第四行下面。就这么简单。

答案4

尝试一下这样的事情,更简单。

<VirtualHost *:80>
    ServerAlias example.com

    Redirect /mantis http://mantis.example.com

    ...
</VirtualHost>

<VirtualHost *:80>
    ServerAlias mantis.example.com

    ...
</VirtualHost>

相关内容