Subdomain redirection with htaccess and a 500 Internal Server Error

本秂侑毒 提交于 2019-12-12 05:06:13

问题


My .htaccess file is, for some reason, causing 500 Internal Server Errors when I'm trying to redirect a subdomain like this:

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. 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:

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 e.g. "test.php" (if you tried to access subdomain.domain.com/test.php), but the whole path from the main domain (i.e. folder/anotherfolder/test.php).

Any ideas what I'm doing wrong?


回答1:


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.




回答2:


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]


来源:https://stackoverflow.com/questions/9521979/subdomain-redirection-with-htaccess-and-a-500-internal-server-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!