带有 url 屏蔽的 Apache 通配符子域

标签 apache .htaccess mod-rewrite url-rewriting

我正在尝试在 apache 中使用 url 屏蔽来处理通配符子域。 正确的重写规则应该达到以下效果:

http://demo.system.dev到 http://system.dev?subdomain=demo

http://sample.system.dev/user/edit/100到http://system.dev/user/edit/100?subdomain=sample

http://debug.system.dev/project/edit/new到 http://system.dev/project/edit/new?subdomain=debug

到目前为止,我的 .htaccess 中有以下规则

RewriteCond %{HTTP_HOST} ^(.*)\.system\.dev
RewriteRule ^(.*?)$ http://system.dev%{REQUEST_URI}?subdomain=%1 [L]

看起来工作正常,只是浏览器 URL 也发生了变化。我希望浏览器 URL 保持不变并在内部路由请求,但我不知道如何实现这一点。

最佳答案

在您的根 .htaccess 中尝试此规则:

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} !(?:^|&)subdomain= [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev$ [NC]
RewriteRule (.*) $0?subdomain=%1 [L,QSA]

关于带有 url 屏蔽的 Apache 通配符子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30239825/

相关文章:

java - Axis2 Soap WS 客户端 : java. lang.NoClassDefFoundError: org/apache/ws/commons/schema/XmlSchema

php - Apache错误日志: Log only for part of server

android - Android 上的简单 Web 服务器

php - 自定义字体不会出现。浏览 MIME、函数和字体系列。没有运气

php - Apache docker 容器 - 无效命令 'RewriteEngine'

WordPress:自定义重写规则 - .htaccess 或 functions.php?

php - URL 重写规则在 php 中不起作用

php - 如何在 php 中更改单元测试日期函数的当前时间

Apache mod_rewrite 添加 anchor 到查询字符串

php - Django URL 重定向所有 PHP 请求