动态子域 +通过 htaccess 定义子域

发布于 2024-09-15 07:13:16 字数 1110 浏览 6 评论 0原文

我讨厌问有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2024-09-22 07:13:16

尝试这些规则:

# 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]

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文