问题
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