如何.htaccess将“http://app-domain.com/example.com?page=podcast”重写为“http://example.com/?page=podcast”?
我正在为多个用户开发应用程序,以 /domain.com 作为自己的页面 ID 进行区分,并希望用户能够将其域名指向同一应用程序服务器,与 /domain.com 情况相比。
RewriteCond %{HTTP_HOST} ^(www\.)?app-domain\.example\.com$ [NC]
RewriteRule ^/?$ http://example.com/?page=podcast/ [R=302,L]
我尝试了该规则的很多变体,但总是收到 500 错误。
您需要两个规则的组合:一个用于重定向长 URL 变体,第二个用于使用短 URL 变体在内部重写传入请求。
这可能是您的起点:
# externally redirect https://app-domain.com/example.com/foo?bar to https://example.com/foo?bar
RewriteCond %{HTTP_HOST} ^(www\.)?app-domain\.com$
RewriteCond %{REQUEST_URI} ^/(\w+\.\w+)/(.*)$
RewriteRule ^ https://%1/%2 [QSA,L,R=301]
# internall rewrite https://example.com/foo?bar to /example.com/foo?bar
RewriteCond %{HTTP_HOST} !^(www\.)?app-domain\.com$
RewriteCond %{HTTP_HOST} ^(www\.)?(\w+\.\w+)
RewriteRule ^ /%2%{REQUEST_URI} [QSA,L]
你可以先在这里尝试一下
最好先使用
R=302
临时 重定向,并在一切按预期工作后将其更改为 R=301
永久 重定向。这可以防止测试时出现客户端缓存问题。尽管如此,您每次都需要使用新的匿名窗口进行测试。
此类规则可以在中央http服务器的虚拟主机配置中实现。如果您无权访问该文件,则可以使用distributed配置文件(“.htaccess”),但这会带来性能劣势,增加复杂性,并且您必须首先启用此类文件的解释(请参阅文档关于
AllowOverride
指令)。