htaccess子域到文件夹重定向

时间:2014-09-07 10:16:12

标签: apache .htaccess mod-rewrite redirect subdomain

我需要将(透明)http://test.mywebsite.com重定向到http://mywebsite.com/test

在Apache2中,我通过在VirtualHost部分添加 ServerAlias * .mywebsite.com 来编辑mywebsite.com.conf文件。然后,我在webmin中重新启动了Apache服务。

最后,我创建了一个新的.htaccess规则:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mywebsite.com$ [NC]
RewriteCond %{HTTP_HOST} ^test\.mywebsite\.com [NC]
RewriteRule (.*) http://www.mywebsite.com/test/$1 [L,R=301]

但是,我发现了 Page not found 错误,现在我不知道该怎么办......

有什么建议吗?感谢。

修改

这是完整的.htaccess文件:

SetEnv REGISTER_GLOBALS 0

# MASK FILES IN DIRECTORIES
Options -Indexes
Options -MultiViews

RewriteEngine on

RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [NC]
RewriteRule ^(.*) http://www.mywebsite.com/$1 [QSA,R=301,L]

RewriteCond %{HTTP_HOST} !^www.mywebsite.com$ [NC]
RewriteCond %{HTTP_HOST} ^test\.mywebsite\.com [NC]
RewriteRule (.*) http://www.mywebsite.com/test/$1 [L,R=301]

编辑2

这是我的VirtualHost文件:

<VirtualHost ***.***.***.***:80>
SuexecUserGroup "#1011" "#1008"
ServerName mywebsite.com
ServerAlias www.mywebsite.com
ServerAlias webmail.mywebsite.com
ServerAlias admin.mywebsite.com
ServerAlias *.mywebsite.com
DocumentRoot /home/mywebsite/public_html
ErrorLog /var/log/virtualmin/mywebsite.com_error_log
CustomLog /var/log/virtualmin/mywebsite.com_access_log combined
ScriptAlias /cgi-bin/ /home/mywebsite/cgi-bin/
ScriptAlias /awstats/ /home/mywebsite/cgi-bin/
DirectoryIndex index.html index.htm index.php index.php4 index.php5
<Directory /home/mywebsite/public_html>
Options -Indexes +IncludesNOEXEC +FollowSymLinks +ExecCGI
allow from all
AllowOverride All
AddHandler fcgid-script .php
AddHandler fcgid-script .php5
FCGIWrapper /home/mywebsite/fcgi-bin/php5.fcgi .php
FCGIWrapper /home/mywebsite/fcgi-bin/php5.fcgi .php5
</Directory>
<Directory /home/mywebsite/cgi-bin>
allow from all
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} =webmail.mywebsite.com
RewriteRule ^(.*) https://mywebsite.com:20000/ [R]
RewriteCond %{HTTP_HOST} =admin.mywebsite.com
RewriteRule ^(.*) https://mywebsite.com:10000/ [R]
RemoveHandler .php
RemoveHandler .php5
IPCCommTimeout 31
FcgidMaxRequestLen 1073741824
<Files awstats.pl>
AuthName "mywebsite.com statistics"
AuthType Basic
AuthUserFile /home/mywebsite/.awstats-htpasswd
require valid-user
</Files>
RedirectMatch /cgi-bin/mailman/([^/\.]*)(.cgi)?(.*) https://mywebsite.com:10000/virtualmin-mailman/unauthenticated/$1.cgi$3
RedirectMatch /mailman/([^/\.]*)(.cgi)?(.*) https://mywebsite.com:10000/virtualmin-mailman/unauthenticated/$1.cgi$3
Alias /pipermail /var/lib/mailman/archives/public
</VirtualHost>

编辑3

我想简化我的问题,所以这是一个新的挑战。这样我们就不再关心文件夹重定向,而只关心变量作为子域。因此,我尝试编写另一条规则,将http://test.mywebsite.com重定向到http://mywebsite.com?sub=test。这是我的(非工作规则):

RewriteCond %{HTTP_HOST} ^test\.mywebsite\.com [NC]
RewriteRule (.*) http://mywebsite.com/index.php?sub=$1 [L,R=301]

此规则将 test.mywebsite.com 重定向到 mywebsite.com/index.php?sub =

0 个答案:

没有答案