.htaccess rewrite subdomains

2019-08-08 02:29发布

问题:

Here is my current code:

RewriteCond %{HTTP_HOST} !^example\.com [NC] 
#RewriteCond %{REQUEST_URI}!^something
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com 
RewriteRule (.*) something/%1/$1 [QSA,L]

My goal is to rewrite http://*.example.com/whatever to http://example.com/something/*/whatever, assuming * is the same for both and whatever is the same for both. However, I believe this is causing an infinite loop somehow, so I added that commented out line (RewriteBase is / btw), but if I uncomment then the entire site is a 500 response code. Without that line, only the subdomains error.

What am I doing wrong, and how can I fix this? I have already configured the server so anything gets redirected to my public_html folder.

EDIT: For clarification, I am trying to do an internal redirect, not external.

EDIT: There is not a flag for internal redirect it seems. Is there any place I can see the actual error message for the broken rewriterule? If I knew what was wrong I would be able to stand a better chance of fixing it.

回答1:

I don't see the rule rewriting the domain ; maybe this is your problem. Using your code, try:

RewriteCond %{HTTP_HOST} !^example\.com [NC] 
#RewriteCond %{REQUEST_URI}!^something
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com 
RewriteRule (.*) http://example.com/something/%1/$1 [QSA,L]

I hope this will help you Jerome WAGNER



回答2:

I added the P (proxy) flag to the rule and it worked.