通配符域和站点根目录中页面的链接
我有一个通配符子域 (ServerAlias *.mydomain.com) 来捕获所有子域请求并将它们重定向到主页,同时在浏览器中保留“假”URL 并将子域(城市名称)作为 URL 参数
.htaccess 传递子域文件夹:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%2.%3/index.php?city=%1 [P,L]
</IfModule>
问题:每个页面都有一个包含一些链接的菜单(register.php、login.php、contact.php),如果您在一个子域中选择其中任何一个,则请求(例如city.mydomain.com/login.php)由条件/规则捕获
我想我需要添加第二组条件/规则,但经过一些测试后找不到正确的条件/规则。我在已经工作的之前添加了这一点:
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/(.*)\.php$ [NC]
RewriteRule ^(.*)$ http://%2.%3/%4 [P,L]
收到错误:
错误的网关! 代理服务器从上游服务器收到无效响应。 代理服务器无法处理请求 GET /register.php。
原因:DNS查找失败的原因:
如果您认为这是服务器错误,请联系网站管理员。
错误502 city.domaim.com
CentOS 5 Parallels Small Business Panel 10.2.0
提前致谢
I have a wildcard subdomain (ServerAlias *.mydomain.com) to catch all subdomains requests and redirecting them to the home page while keeping the "fake" URL in the browser and passing the subdomain (city name) as a URL parameter
.htaccess in the subdomain folder:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%2.%3/index.php?city=%1 [P,L]
</IfModule>
The problem: there is a menu with some links in every page (register.php, login.php, contact.php) and if you select any of them while being in one subdomain, the request (city.mydomain.com/login.php for example) is captured by the condition/rule
I guess I need to add a second set of condition(s)/rule but after some tests can't find the right one. I added this before the one already working:
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/(.*)\.php$ [NC]
RewriteRule ^(.*)$ http://%2.%3/%4 [P,L]
receiving the error:
Bad Gateway!
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /register.php.
Reason: DNS lookup failure for:
If you think this is a server error, please contact the webmaster.
Error 502
city.domaim.com
CentOS 5
Parallels Small Business Panel 10.2.0
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 mod_rewrite 可能会使这个过程变得过于复杂。我建议简单地解析/验证子域(城市)并使用直接 PHP 设置一个常量。例如在配置/初始化文件中。最后,您将对子域拥有更大的控制权,尤其是在验证的情况下。
亲吻:)
You may be over complicating this by using mod_rewrite. I'd suggest simply to parse/validate the subdomain (city) and set a constant with straight PHP. For example in a config/init file. In the end, you'll have far greater control over the subdomain, especially in the case of validation.
KISS :)
这个错误是一个幼稚的错误,我忽略了替换字符串中对 RewriteCond 模式 (%N) 的反向引用,引用最后一个匹配的。然后,在我原来的规则中,%2、%3 和 %4 没有任何意义
这是 .htaccess 的工作原理:
此外,[P] 标志意味着 [L]
教训:RTFM! :-)
The error was a naive one, I overlooked that back-references in the substitution string to RewriteCond pattern (%N), refer to the last one matched. Then, in my original rule, %2, %3 and %4 didn't make any sense
Here is the .htaccess working:
Besides, the [P] flag implies [L]
The lesson: RTFM! :-)