问题
I have a problem with one of my .htaccess rewriterule:
Server details:
Server runs LiteSpeed Webserver not Apache, according to my host provider it's 100% compatible with apache mod_rewrite.
Problem:
I get 500 Internal Server Error when i use this code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).mysite.com$ [NC]
RewriteRule ^(.*)$ %2/$1 [L,QSA,NC]
</IfModule>
When I use this code, it pretty much does what it is suppose to do it redirects traffic from any sub-domain to a corresponding sub-folder with file request also. No Error, no nothing it works OK but it also changes the browser's address which i don't wish.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).mysite.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/%2/$1 [L,QSA,NC]
</IfModule>
Any ideas?
Editetd : One thing i would like to mention is that i use wildcard DNS the subdomain don't realy exist. Editted yet again : RewriteRule ^(.*)$ %2/index.php [L,QSA,NC] if i use this rule and try to acces a http://sub.mysite.com/whatever it succesfuly redirects it to http://mysite.com/sub/index.php i don't get it why it does not work with $1 instead of index.php when i change them i get 500 internal server error.
Got the solution now :
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{HTTP_HOST} ^(.*)mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.* [NC]
RewriteRule ^(.*)$ http://www.%1mysite.com/$1 [R=301,L,QSA] [NC]
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).mysite.com$ [NC]
RewriteCond %2==$1 !^(.*)==\1
RewriteRule ^(.*) %2/$1$2$3 [L,QSA,NC]
</IfModule>
回答1:
This is an interesting generic problem which merits a generic solution, so how about:
#
# Rule matches ('fred','','') for pattern 'fred'
# ('123','/',fred') for pattern '123/fred'
# At cond 2 %2 is the subdomain e.g. 123 for www.123.mysite.com
# Hence cond 3 pattern is 123==123 for www.123.mysite.com/123/fred
# and this will match ^(.*?)==\1 or fail its negation
#
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).mysite.com$ [NC]
RewriteCond %2==$1 !^(.*?)==\1
RewriteRule ^([^/]*)(/?)(.*) %1/$1$2$3 [L]
回答2:
I think your rule should be re-written like this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.mysite\.com$ [NC]
RewriteRule ^ http://mysite.com/%2%{REQUEST_URI} [L,P]
P flag will make it act like a proxy and will keep the URL in browser to subdomain.mysite.com/whatever
instead of mysite.com/subdomain/whatever
回答3:
This is because it is creating an infinite rewrite loop.
123.mysite.com/
get rewritten to 123.mysite.com/123/
, which gets rewritten to 123.mysite.com/123/123/
etc.
Try using this to prevent this loop:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).mysite.com$ [NC]
RewriteRule ^(.*)$ %2/$1 [L,QSA,NC]
来源:https://stackoverflow.com/questions/9023448/htaccess-500-error-rewriterule-not-working-as-it-should