如何修改 .htaccess 文件以始终重定向到 www
我想修改我的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用下面的代码。它可以用于任何域名。您只需将其输入到 .htaccess 文件中。
(编辑为将所有代码放在同一块中)
I use the code below. It can be used for any domain name. You just need to enter it in your .htaccess file.
(edited to have all the code in the same block)
在
RewriteEngine on
之后立即添加类似的内容:Add something like this immediately after
RewriteEngine on
:有两种方法可用
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]
如果您想将 example.com 重定向到 www.example.com 您可以尝试以下代码
If you want redirect example.com to www.example.com you can try below code