通过htaccess的动态子域+定义子域 [英] Dynamic subdomains + defined subdomains via htaccess

查看:126
本文介绍了通过htaccess的动态子域+定义子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我讨厌问mod_rewrite的问题,但我似乎无法得到这些规则正常工作。我有它设置和揣摩过一次,但几年过去了,现在它只是没有发挥很好,我的mod_rewrite的知识是回到Joey:嗯......

从本质上讲就是我想要做的是对的主域名,example.com力WWW。 (或者强迫任何WWW甚至会更好,但我没有远程能够得到的正常工作以及动态子域)。

如果存在my.example.com规则,然后按照它们相应的

为别的,即:* .example.com的,传递到另一个文件解析

规则我已经当前处理迫使WWW,妥善处理动态子域,但完全忽略my.example.com规则,处理它作为通配符子域。看来我可以得到我的任何一个规则工作正常,但试图让他们一起工作,它只是作为一个混蛋的时候。如果有人知道要得到这个工作正常,将是超级AP preciated。

 选项+了FollowSymLinks
RewriteEngine叙述上
的RewriteBase /

#在主力WWW,没有强制的www会更好。
的RewriteCond%{HTTP_HOST}!^(。*)\。example.com $ [NC]
重写规则^(。*)$ http://www.example.com/$1 [R = 301,L]

#my.example.com定义子域名
的RewriteCond%{HTTP_HOST} ^!我\ .EXAMPLE \ .COM $ [NC]
。重写规则^ * $  -  [S = 3]
重写规则^ $ /mydirectory/index.php [L]
重写规则^类/([^ /] +)/ $ /mydirectory/index.php?category=$1 [L]
重写规则^递交/?$ /mydirectory/process.php [L]

#* .example.com的
的RewriteCond%{HTTP_HOST} ^(。*)\。com $ [NC]
的RewriteCond%1!^(WWW)\。例子$ [NC]
重写规则^([^ /] +)/ $ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]
 

解决方案

请尝试以下规则:

 在主#删除WWW
的RewriteCond%{HTTP_HOST} ^ WWW \ .EXAMPLE \ .COM $ [NC]
重写规则^(。*)$ http://example.com/$1 [R = 301,L]

#my.example.com定义子域名
的RewriteCond%{HTTP_HOST} ^!我\ .EXAMPLE \ .COM $ [NC]
。重写规则^ * $  -  [S = 3]
重写规则^ $ /mydirectory/index.php [L]
重写规则^类/([^ /] +)/ $ /mydirectory/index.php?category=$1 [L]
重写规则^递交/?$ /mydirectory/process.php [L]

#* .example.com的
的RewriteCond%{HTTP_HOST} ^(。*)\。例如\ .COM $ [NC]
的RewriteCond%1!^ WWW $ [NC]
重写规则^([^ /] +)/ $ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]
 

第一条规则现在将重定向+ www.example.com *来的 example.com 的。而第三条规则将捕获任何请求一个不存在的主机。

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]

解决方案

Try these rules:

# remove www on main
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://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} ^(.*)\.example\.com$ [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]

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.

这篇关于通过htaccess的动态子域+定义子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆