优化 htaccess 通配符子域代码
我的通配符子域在我的服务器上完美运行,但想优化 .htaccess 代码以使其更便携。我不知道该怎么做。
我想用正则表达式匹配替换特定域名和扩展名(下面代码中的 DOMAIN 和 .com),以便在对其他域使用通配符代码时可以轻松移植。 所以它应该是domain.com 和domain2.net 的替代品。这是我使用的代码:
# Wildcard Subdomain
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+)\.DOMAIN\.com [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://DOMAIN.com/$1?a=%2 [R=301,QSA,L]
它与您看到的通常版本略有不同,因为我添加了 www. + 子域条件。很多情况下,人们输入 www.sub.domain.com 会导致服务器中断。所以我检查了 www.并在重定向之前将其删除。
感谢您的帮助!
I have Wildcard Subdomains working perfectly on my server, but would like to optimize the .htaccess code to make it more portable. I'm not sure how to do it.
I would like to replace the specific domain name and extension (DOMAIN and .com in the code below) with a regex match so that when using the wildcard code for other domains it is easy to port. So it should be a drop in replacement for domain.com and domain2.net. Here's the code I use:
# Wildcard Subdomain
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+)\.DOMAIN\.com [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://DOMAIN.com/$1?a=%2 [R=301,QSA,L]
It's a little different than the usual version you see, because I've added the exception for the www. + subdomain condition. It many cases people enter www.sub.domain.com which breaks on the server. So I check for the www. and remove it before redirecting.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的方式:
如果您只想从域中删除 www:
The way you currently have it:
If you just want to strip the www from the domain: