如何修改 .htaccess 文件以始终重定向到 www

发布于 2024-10-02 04:10:11 字数 392 浏览 0 评论 0原文

我想修改我的 .htaccess 文件,以便当有人进入我的网站而不输入 www 时,该网站总是将他们重定向到 www 版本。例如,如果我的网址是 www.abc.com,而他们只需输入 abc.com,我想将他们重定向到 abc.com。

这是我当前的 htaccess 文件:

<IfModule mod_rewrite.c>
   RewriteBase /
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]  
</IfModule>

通常我知道如何进行重定向,但我遇到了问题,因为它已经有那几行了。

I would like to modify my .htaccess file so that when someone comes into my site without typing www the site always redirects them to the www version. For example, if my url is www.abc.com and they just type abc.com, I want to redirect them to abc.com.

Here is my current htaccess file:

<IfModule mod_rewrite.c>
   RewriteBase /
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]  
</IfModule>

Normally I know how to do the redirect, but im having issues since it already has those few lines in there.

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

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

发布评论

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

评论(4

若水般的淡然安静女子 2024-10-09 04:10:11

我使用下面的代码。它可以用于任何域名。您只需将其输入到 .htaccess 文件中。

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

(编辑为将所有代码放在同一块中)

I use the code below. It can be used for any domain name. You just need to enter it in your .htaccess file.

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

(edited to have all the code in the same block)

笑梦风尘 2024-10-09 04:10:11

RewriteEngine on 之后立即添加类似的内容:

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

Add something like this immediately after RewriteEngine on:

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
﹏雨一样淡蓝的深情 2024-10-09 04:10:11

有两种方法可用

1) 使用 mod_alias apache 模块

Redirect permanent /something http://yourwebsite.com/something

2 中的 .htaccess/http.conf/yourwebsite.conf 中添加以下条目

) 在 Web 服务器配置目录RewriteEngine
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule ^(.*)$ http://www.yourwebsite.com$1 [R=permanent,L]

There are two methods available

1) use mod_alias apache module

Redirect permanent /something http://yourwebsite.com/something

2) Add the following entry in your .htaccess / http.conf / yourwebsite.conf in the webserver configuarion directory

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule ^(.*)$ http://www.yourwebsite.com$1 [R=permanent,L]

橪书 2024-10-09 04:10:11

如果您想将 example.com 重定向到 www.example.com 您可以尝试以下代码

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

If you want redirect example.com to www.example.com you can try below code

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