使用 htaccess 的通配符子域

发布于 2024-09-07 01:08:36 字数 509 浏览 3 评论 0原文

我在让下面的代码语句按预期工作时遇到一些麻烦。代码的第一部分一切都很好。当用户访问不带“www”的 example.com 时,他会被 301 重定向到 www.example.com 以保护传入链接。

同时我正在尝试使用子域。如果用户点击 candybar.example.com 我希望加载页面 test.php?subdomain=candybar 。尽管这部分似乎不起作用,但我的用户正在使用索引页面。

我希望有人知道我做错了什么 - 提前致谢!

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ test.php?subdomain=$1 [L]

I'm having some trouble getting the code statement beneath to work as intended. The first part of the code is all good. When a user hits example.com without 'www', he's being 301 redirected to www.example.com to protect the ingoing links.

At the same time I'm trying to use subdomains. If a user hits candybar.example.com I want the page test.php?subdomain=candybar to be loaded. Though this part doesn't seem to work, my users are being served the index page instead.

I hope someone has an idea what I'm doing wrong - thanks in advance!

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ test.php?subdomain=$1 [L]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

烈酒灼喉 2024-09-14 01:08:36

我自己找到了解决方案:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^$ test.php [L]

I found the solution myself:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^$ test.php [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文