how to modify .htaccess file to always redirect to

2020-02-04 06:04发布

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.

4条回答
小情绪 Triste *
2楼-- · 2020-02-04 06:18

Add something like this immediately after RewriteEngine on:

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
查看更多
走好不送
3楼-- · 2020-02-04 06:19

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]
查看更多
We Are One
4楼-- · 2020-02-04 06:22

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)

查看更多
闹够了就滚
5楼-- · 2020-02-04 06:37

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]

查看更多
登录 后发表回答