htaccess WWW to no-WWW not working

2019-08-29 04:39发布

Ok I know this hasn't been answered a lot, but I've been using a lot of various different solutions but none of them are working for me.

I'm trying to use mod_rewrite in my htaccess to redirect all www.domain.com to domain.com - standard enough.

The site is a wordpress site, and it's always worked for me without any problems using any of the following

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

Or

RewriteEngine On
RewriteCond %{HTTP_HOST}  ^www.domain.com [nocase]
RewriteRule ^(.*)         http://domain.com/$1 [last,redirect=301]

I've tried a few other ones as well but none seem to work!

It may also be worth bearing in mind the following are also in the htaccess from wordpress

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

I have also checked to see if mod_rewrite is even enabled and it appears to be based on the methods in tried here How to check if mod_rewrite is enabled in php? (Christian Roy's solution)

The server is a small droplet with Ubuntu 12.10 hosted with digital ocean if that helps? I followed this guide to ensure .htaccess was activated https://www.digitalocean.com/community/articles/how-to-use-the-htaccess-file (main change was changing AllowOveride to ALL)

Anyone any ideas? I'm lost at this stage!

Thanks

Updated htaccess file contents as requested

 # BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST}  ^www\. [NC]
RewriteRule ^(.*)$   http://domain.com/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

**Updated with Virtual Host File **

<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /home/wordpress/public_html

Servername domain.com

ServerAlias www.domain.com

<Directory /home/wordpress/public_html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All   
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

2条回答
闹够了就滚
2楼-- · 2019-08-29 05:10
RewriteEngine On
RewriteCond %{HTTP_HOST}  ^www\. [NC]
RewriteRule ^(.*)$   http://domain.com/$1 [R=301,L]

should do it. As advised before, it should probably be done before WP-specific rewrites.

查看更多
\"骚年 ilove
3楼-- · 2019-08-29 05:23

It will work but you need to make sure 2 things:

  1. You place www removal rule as the first rule before regular WP rules.
  2. Update WP permalink settings to have blog/home address as without www
查看更多
登录 后发表回答