【问题标题】:Subdomain redirection with htaccess and a 500 Internal Server Error使用 htaccess 进行子域重定向和 500 内部服务器错误
【发布时间】:2012-03-01 18:38:32
【问题描述】:

当我尝试像这样重定向子域时,由于某种原因,我的 .htaccess 文件会导致 500 个内部服务器错误:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com [NC]
RewriteRule ^(.*)$ folder/anotherfolder/$1 [L]

我当前的主机不会自己重定向子域,所以我必须这样做。我想要实现的是,当用户输入http://subdomain.domain.com/ 时,他真的看到了http://domain.com/folder/anotherfolder。但是,我不希望地址栏上的域本身发生变化。子域本身显然是主域的别名,因此指向与主域相同的文件夹。

当正则表达式结果用作 GET 时,上面的代码也可以工作,例如:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com [NC]
RewriteRule ^(.*)$ folder/anotherfolder/index.php?p=$1 [L]

但是,GET "p" 的值不是例如“test.php”(如果您尝试访问 subdomain.domain.com/test.php),但主域的整个路径(即文件夹/另一个文件夹/test.php)。

任何想法我做错了什么?

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite subdomain


    【解决方案1】:

    啊,似乎是由内部重定向循环引起的。通过这样做修复:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^subdomain.domain.com [NC]
    RewriteRule ^folder/anotherfolder/ - [L]
    RewriteRule (.*) folder/anotherfolder/$1 [L]
    

    有趣的是,答案通常是在您询问某人后立即显示出来的。

    【讨论】:

      【解决方案2】:

      将您的代码更改为:

      RewriteEngine On
      
      RewriteCond %{QUERY_STRING} !^p= [NC]
      RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
      RewriteRule ^(.*)$ folder/anotherfolder/index.php?p=$1 [L]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-30
        • 2016-12-29
        • 1970-01-01
        • 2016-09-22
        • 2017-04-05
        • 1970-01-01
        • 2013-02-06
        相关资源
        最近更新 更多