.htaccess mod_rewrite 重定向

发布于 2025-01-01 22:16:59 字数 1118 浏览 1 评论 0原文

示例#1(按预期工作):

RewriteCond %{HTTP_HOST} ^www.hanoichurch.com$ [OR]
RewriteCond %{HTTP_HOST}     ^hicvn.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.hicvn.com$ [OR]
RewriteCond %{HTTP_HOST}     ^hicvn.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.hicvn.org$ [OR]
RewriteCond %{HTTP_HOST}     ^hanoiworship.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.hanoiworship.com$
RewriteCond %{REQUEST_FILENAME} !^google
RewriteRule ^/?(.*)$ "http\:\/\/hanoichurch\.com\/$1" [R=301,L]

示例#2(不起作用*):

RewriteCond %{HTTP_HOST} ^www.nathanbrauer.com$ [OR]
RewriteCond %{HTTP_HOST}     ^nathanbrauer.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.nathanjbrauer.com$
RewriteCond %{REQUEST_FILENAME} !^google
RewriteRule ^/?(.*)$ "http\:\/\/nathanjbrauer\.com\/$1" [R=301,L]

*静默失败(访问 www.nathanbrauer.com 只是加载页面,不会重定向到 nathanjbrauer.com)。

一百万谢谢!

===更新===

我最终做的是使用这段代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^nathanjbrauer\.com$
RewriteCond %{REQUEST_FILENAME} !^google
RewriteRule ^ http://nathanjbrauer.com%{REQUEST_URI} [R=301,L]

Sample #1 (works as expected):

RewriteCond %{HTTP_HOST} ^www.hanoichurch.com$ [OR]
RewriteCond %{HTTP_HOST}     ^hicvn.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.hicvn.com$ [OR]
RewriteCond %{HTTP_HOST}     ^hicvn.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.hicvn.org$ [OR]
RewriteCond %{HTTP_HOST}     ^hanoiworship.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.hanoiworship.com$
RewriteCond %{REQUEST_FILENAME} !^google
RewriteRule ^/?(.*)$ "http\:\/\/hanoichurch\.com\/$1" [R=301,L]

Sample #2 (does not work*):

RewriteCond %{HTTP_HOST} ^www.nathanbrauer.com$ [OR]
RewriteCond %{HTTP_HOST}     ^nathanbrauer.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.nathanjbrauer.com$
RewriteCond %{REQUEST_FILENAME} !^google
RewriteRule ^/?(.*)$ "http\:\/\/nathanjbrauer\.com\/$1" [R=301,L]

*Fails silently (visiting www.nathanbrauer.com simply loads the page and doesn't redirect to nathanjbrauer.com).

A million thanks!

=== Update ===

What I ended up doing was using this code:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^nathanjbrauer\.com$
RewriteCond %{REQUEST_FILENAME} !^google
RewriteRule ^ http://nathanjbrauer.com%{REQUEST_URI} [R=301,L]

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

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

发布评论

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

评论(1

风渺 2025-01-08 22:16:59

您的示例 1 代码也可以重构,但我只会建议修复您的示例 2 代码,因为这不适合您:

RewriteCond %{HTTP_HOST} ^(www\.)?nathanbrauer\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.nathanjbrauer\.com$
RewriteCond %{REQUEST_FILENAME} !^google [NC]
RewriteRule ^ http://nathanjbrauer.com%{REQUEST_URI} [R=301,L]

Your sample1 code can also be refactored but I will only suggest fixes to your sample 2 code since this is not working for you:

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