htaccess and no www

2019-04-17 10:16发布

I am trying to redirect my whole site to non-www

here is the htaccess code I am using

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

# no www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.akorra\.com$ [NC]
RewriteRule ^.*$ http://akorra.com%{REQUEST_URI} [R=301,L]

any ideas

5条回答
神经病院院长
2楼-- · 2019-04-17 10:51

Here is what you need to add to your .htaccess

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

However, if you are working with Wordpress as the engine that powers your entire site, just update the permalink structure and all internal settings with your domain name in it to remove the www.

EDIT:

I thought that was different when I wrote the answer Sorry. Try moving your non www rule to the top...

# no www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.akorra\.com$ [NC]
RewriteRule ^.*$ http://akorra.com%{REQUEST_URI} [R=301s,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

AGAIN keep in mind if Wordpress is set to produce links with a www then nothing will stop it from reverting everything back to having a www even with a correct rewrite rule.

More Wordpress details:

Check the settings in wp/wp-admin/options-general.php ...

  • Be sure to remove the www in WordPress address (URL)
  • Be sure to remove the www in Blog address (URL)

AND then update your premalink structure in wp-admin/options-permalink.php so the changes are reflected.

After all that is said and done, be sure Wordpress did not overwrite new code in your .htaccess file.

查看更多
够拽才男人
3楼-- · 2019-04-17 10:55

Hi I think you need to adjust your script as follows for the rewriting to work:

Options +FollowSymlinks
RewriteEngine on

I believe that the follow symlinks absolutely has to be included for url rewriting to work properly.

More advice on mod_rewrite here:

http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html

Rob

查看更多
迷人小祖宗
4楼-- · 2019-04-17 10:56

I think your rules need to between the module tags.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# no www
RewriteCond %{HTTP_HOST} ^www\.akorra\.com$ [NC]
RewriteRule ^.*$ http://akorra.com%{REQUEST_URI} [R=301,L]

# BEGIN WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

</IfModule>
查看更多
虎瘦雄心在
5楼-- · 2019-04-17 10:56
RewriteEngine On
RewriteBase /

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

# WordPres
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

This should work

查看更多
一夜七次
6楼-- · 2019-04-17 10:59

I suggest to use this piece of code for removing www from your website:

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

it's more generic.

and in your example it's best to use this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # no www
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    # BEGIN WordPress
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress
</IfModule>
查看更多
登录 后发表回答