具有 url 掩码的 Apache 通配符子域
Apache wildcard subdomains with url masking
我正在尝试在 apache 中使用 url 掩码处理通配符子域。
正确的重写规则应该达到以下目的:
http://demo.system.dev
到 to http://system.dev?subdomain=demo
http://sample.system.dev/user/edit/100
到 to http://system.dev/user/edit/100?subdomain=sample
http://debug.system.dev/project/edit/new
到 to 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 保持不变并在内部路由请求,但我不确定如何实现。
您不能在内部重写到另一个域。因此,当您转到您的子域时,它将重定向到您拥有的主域。所以你需要使用 relative URL 看看它是否适合你。
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.)?system\.dev [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?subdomain=%1 [L]
在您的根 .htaccess 中尝试此规则:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !(?:^|&)subdomain= [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev$ [NC]
RewriteRule (.*) [=10=]?subdomain=%1 [L,QSA]
我正在尝试在 apache 中使用 url 掩码处理通配符子域。 正确的重写规则应该达到以下目的:
http://demo.system.dev
到 to http://system.dev?subdomain=demo
http://sample.system.dev/user/edit/100
到 to http://system.dev/user/edit/100?subdomain=sample
http://debug.system.dev/project/edit/new
到 to 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 保持不变并在内部路由请求,但我不确定如何实现。
您不能在内部重写到另一个域。因此,当您转到您的子域时,它将重定向到您拥有的主域。所以你需要使用 relative URL 看看它是否适合你。
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.)?system\.dev [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?subdomain=%1 [L]
在您的根 .htaccess 中尝试此规则:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !(?:^|&)subdomain= [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev$ [NC]
RewriteRule (.*) [=10=]?subdomain=%1 [L,QSA]