我正在尝试在 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/