.htaccess rewrite subdomains

无人久伴 提交于 2019-12-11 08:34:40

问题


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.



来源:https://stackoverflow.com/questions/2579537/htaccess-rewrite-subdomains

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