动态子域 +通过 htaccess 定义子域
我讨厌问有关 mod_rewrite 的问题,但我似乎无法让这些规则正常工作。我之前已经设置并弄清楚了一次,但是几年过去了,现在它运行得不太好,我的 mod_rewrite 知识又回到了呃……
本质上我想做的是在主域上强制 www ,example.com。 (或者强制不使用 www 会更好,但我还无法远程使其与动态子域一起正常工作)。
如果 my.example.com 规则存在,则相应地遵循它们
对于其他任何内容,即:*.example.com,传递到另一个文件进行解析。
我当前的规则可以强制使用 www,并正确处理动态子域,但完全忽略 my.example.com 规则,将其作为通配符子域处理。似乎我可以让我的任何一条规则正常工作,但当试图让它们一起工作时,那就是一个混蛋。如果有人知道必须使其正常工作,我将不胜感激。
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# force www on main, force no www would be better.
RewriteCond %{HTTP_HOST} !^(.*)\.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# my.example.com defined subdomain
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^.*$ - [S=3]
RewriteRule ^$ /mydirectory/index.php [L]
RewriteRule ^category/([^/]+)/?$ /mydirectory/index.php?category=$1 [L]
RewriteRule ^submit/?$ /mydirectory/process.php [L]
# *.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.examples$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]
I hate asking questions about mod_rewrite, but I can't seem to get these rules working properly. I had it setup and figured out once before, but a few years have passed and now it's just not playing nice and my mod_rewrite knowledge is back to uhhhh......
Essentially what I want to do is force www on the main domain, example.com. (or forcing no www would be even better, but I haven't remotely been able to get that working properly along with dynamic subdomains).
If my.example.com rules exist, then follow them accordingly
For anything else, ie: *.example.com, pass to another file to parse.
The rules I have currently handles forcing the www, and properly handles the dynamic subdomain, but completely ignores my.example.com rules, handling it as a wildcard subdomain. It seems I can get any one of my rules working properly, but when trying to get them working together it's just being a bastard. If anyone knows have to get this working properly it would be super appreciated.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# force www on main, force no www would be better.
RewriteCond %{HTTP_HOST} !^(.*)\.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# my.example.com defined subdomain
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^.*$ - [S=3]
RewriteRule ^$ /mydirectory/index.php [L]
RewriteRule ^category/([^/]+)/?$ /mydirectory/index.php?category=$1 [L]
RewriteRule ^submit/?$ /mydirectory/process.php [L]
# *.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.examples$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这些规则:
第一条规则现在会将 +www.example.com* 重定向到 example.com。第三条规则将捕获对不存在主机的任何请求。
Try these rules:
The first rule will now redirect the +www.example.com* to example.com. And the third rule will catch any request to a non existing host.