简体   繁体   English

htaccess 通过子域访问文件夹

[英]htaccess access to folders via subdomains

I am trying to make a rule in .htaccess to access a folder via subdomain alias (with parameters).我正在尝试在 .htaccess 中制定规则,以通过子域别名(带参数)访问文件夹。

sub.example.com => example.com/sub/index.php
sub.example.com/language-fr => example.com/sub/inner_page.php?lang=fr

I found a simple solution for subdomains with parameters.我为带有参数的子域找到了一个简单的解决方案。

RewriteCond %{HTTP_HOST} ^sub.example.com
RewriteRule ^(.*)$ /sub/inner_page.php?lang=$1 [L,NC,QSA]

This works correctly.这可以正常工作。 When i browse to sub.example.com/language-fr i am getting the contents of example.com/sub/inner_page.php?lang=fr But this doesn't work when i browse sub.example.com i see error ERR_TOO_MANY_REDIRECTS.当我浏览到 sub.example.com/language-fr 时,我得到了 example.com/sub/inner_page.php?lang=fr 的内容但是当我浏览 sub.example.com 时这不起作用我看到错误 ERR_TOO_MANY_REDIRECTS .

Please tell me how to correctly specify the rules for accessing sub.example.com and internal pages with parameters like sub.example.com/language-fr?请告诉我如何正确指定访问 sub.example.com 和带有 sub.example.com/language-fr 等参数的内部页面的规则?

I actually cannot explain in detail why you enter a redirection loop here.. but this should be a working solution.我实际上无法详细解释为什么您在此处输入重定向循环..但这应该是一个可行的解决方案。

RewriteCond one after another add each other using AND (unless you use the [OR] flag) RewriteCond一个接一个地使用 AND 相加(除非你使用[OR]标志)

I would use this on the %{REQUEST_URI} to prevent each case to be detected by the other ( ^$ means "empty"):我会在%{REQUEST_URI}上使用它来防止每个案例被另一个案例检测到( ^$表示“空”):

RewriteCond %{HTTP_HOST} ^sub.example.com
RewriteCond %{REQUEST_URI} !^$
RewriteRule ^(.+)$ /sub/inner_page.php?lang=$1 [L,NC,QSA]

RewriteCond %{HTTP_HOST} ^sub.example.com
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^$ /sub/index.php [L,NC,QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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