重定向如果域不正确

发布于 2025-02-09 21:29:27 字数 192 浏览 2 评论 0原文

我需要将错误域错误的URL重定向到正确的域。

伪代码示例:

if (domain != "www.correctdomain.com")
    redirect("www.correctdomain.com")

如何使用.htaccess文件执行此操作?

I need to redirect URLs with wrong domains to the correct domain.

Pseudo code example:

if (domain != "www.correctdomain.com")
    redirect("www.correctdomain.com")

How can I do this with a .htaccess file?

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

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

发布评论

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

评论(2

述情 2025-02-16 21:29:28

您可以使用指令...

<If "%{HTTP_HOST} != 'www.example.com'">
Redirect / http://www.example.com/
</If>

或mod_rewrite进行此操作。请参阅 http://httpd.apache.org/docce.org/docs/current/current/rewrite/reprite/rempapping.htmapping.html

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

You can do this with an If directive...

<If "%{HTTP_HOST} != 'www.example.com'">
Redirect / http://www.example.com/
</If>

Or mod_rewrite. See http://httpd.apache.org/docs/current/rewrite/remapping.html

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/?(.*)         http://www.example.com/$1 [L,R,NE]
2025-02-16 21:29:28

最好将301重定向到SEO目的:

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

It's better to make 301 redirect in SEO purposes:

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