简体   繁体   English

带有htaccess的子域重定向和500内部服务器错误

[英]Subdomain redirection with htaccess and a 500 Internal Server Error

My .htaccess file is, for some reason, causing 500 Internal Server Errors when I'm trying to redirect a subdomain like this: 由于某种原因,我的.htaccess文件在尝试重定向这样的子域时导致500个内部服务器错误:

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

My current host doesn't redirect subdomains by themselves, so I have to do it this way. 我当前的主机不会自行重定向子域,因此我必须这样做。 What I'm trying to achieve is that when a user enters http://subdomain.domain.com/ , he really sees http://domain.com/folder/anotherfolder . 我想要实现的是,当用户输入http://subdomain.domain.com/ ,他实际上看到了http://domain.com/folder/anotherfolder I, however, don't want the domain itself on the address bar to change. 但是,我不想更改地址栏上的域本身。 The subdomain itself is, apparently, an alias of the main domain and thus points to the same folder as the main domain. 显然,子域本身是主域的别名,因此指向与主域相同的文件夹。

The above code also works when the regex result it used as a GET, as such: 上面的代码在将正则表达式用作GET时也适用,例如:

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

The value of the GET "p", however, is not eg "test.php" (if you tried to access subdomain.domain.com/test.php), but the whole path from the main domain (ie folder/anotherfolder/test.php). 但是,GET“ p”的值不是“ test.php”(如果您尝试访问subdomain.domain.com/test.php),而是来自主域的整个路径(例如,文件夹/ otherfolder / test.php)。

Any ideas what I'm doing wrong? 有什么想法我做错了吗?

Ah, seems to have been caused by an internal redirection loop. 嗯,似乎是由内部重定向循环引起的。 Fixed by doing this: 通过执行此操作来解决:

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

Funny how the answer usually reveals itself right after you've asked someone. 有趣的是,通常在您询问某人后,答案通常会如何显示出来。

Change your code to this: 将代码更改为此:

RewriteEngine On

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM