简体   繁体   中英

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:

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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