通配符域和站点根目录中页面的链接

发布于 2024-12-01 01:18:32 字数 936 浏览 2 评论 0原文

我有一个通配符子域 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記柔刀 2024-12-08 01:18:32

使用 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 :)

还不是爱你 2024-12-08 01:18:32

这个错误是一个幼稚的错误,我忽略了替换字符串中对 RewriteCond 模式 (%N) 的反向引用,引用最后一个匹配的。然后,在我原来的规则中,%2、%3 和 %4 没有任何意义

这是 .htaccess 的工作原理:

<IfModule mod_rewrite.c>
 Options +FollowSymLinks
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
 RewriteCond %{REQUEST_URI} ^/(.*)\.php$ [NC]
 RewriteRule ^(.*)$ http://mydomain.com/%1.php [P]
 RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
 RewriteRule ^(.*)$ http://%2.%3/index.php?city=%1 [P]
</IfModule>

此外,[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:

<IfModule mod_rewrite.c>
 Options +FollowSymLinks
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
 RewriteCond %{REQUEST_URI} ^/(.*)\.php$ [NC]
 RewriteRule ^(.*)$ http://mydomain.com/%1.php [P]
 RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$ [NC]
 RewriteRule ^(.*)$ http://%2.%3/index.php?city=%1 [P]
</IfModule>

Besides, the [P] flag implies [L]

The lesson: RTFM! :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文