0

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

Community
  • 1
  • 1
robobobobo
  • 739
  • 1
  • 9
  • 19

2 Answers2

0

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
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • As I said in response to Phil Perry, I have placed the rules at the very top of the file and checked the permalinks to make sure they are all http://domain.com , restarted apache and still nothing! Is there anything with the htaccess file that I could be missing? I think it's fine though for the wordpress bits so that's what's confusing me – robobobobo Dec 31 '13 at 13:02
  • OK then run your request in firebug and see whether you get 301 response twice or once. – anubhava Dec 31 '13 at 13:11
  • Also try running in a different browser to avoid 301 caching issues. – anubhava Dec 31 '13 at 13:25
  • There was no 301 response at all in Firebug as far as I could see – robobobobo Jan 01 '14 at 13:56
  • So when you try to open `http://www.domain.com/` it remains unchanged even in a new browser? Have you changed all the URLs to be non-www in WP permalink settings? – anubhava Jan 01 '14 at 14:02
  • Yep that's correct, it doesn't even change, but the wordpress pretty permalinks are working fine (which makes me think the htaccess file is working partially at least?), and everything is set to http://domain.com in all settings and permalinks – robobobobo Jan 01 '14 at 14:16
  • Ok do `www.domain.com` and `domain.com` point to same `VirtualHost` entry in your Apache config? – anubhava Jan 01 '14 at 14:19
  • Ok so my apache config in `etc/apache2/apache2.conf` showed this for virtual host configurations `Include sites-enabled/` and in that file I have ` ServerAdmin webmaster@MyServer DocumentRoot /home/wordpress/public_html Servername MyServer ServerAlias www.MyServer ServerAdmin webmaster@MyServer DocumentRoot /home/wordpress/public_html Servername 95.##.43.### ` Something tells me that maybe this isn't quite right? – robobobobo Jan 01 '14 at 14:43
  • Yes it doesn't look correct to me either. There should be only one `VirtualHost` entry with `ServerName` and `ServerAlias` lines. – anubhava Jan 01 '14 at 14:46
  • I have changed it now, and also my servers hostname to be domain.com, I'll add the virtual host entry to the main question now. Still not working for me! – robobobobo Jan 02 '14 at 20:51
  • Test whether this .htaccess is indeed working for wwww site. same Enter some random text on top of your `.htaccess` and see if it generates 500 (internal server) error or not for `http://www.domain.com/` request? – anubhava Jan 03 '14 at 03:59
  • Adding random text on top of the .htaccess file does indeed generate a 500 internal server error for 'domain.com', but nothing is displayed or the 'www.domain.com' just the page cannot be found error – robobobobo Jan 03 '14 at 17:26
  • Ok that confirms my suspicion that `www.domain.com` is not hitting same DocumentRoot as `domain.com` and that explains why your rule isn't working. – anubhava Jan 03 '14 at 17:30
0
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.

Phil Perry
  • 2,126
  • 14
  • 18
  • I'm afraid that still hasn't worked. I've placed that at the very top of my htaccess file before everything else, then restarted apache and still nothing! I also have both the wordpress url and site url set to http://domain.com as suggested by anubhava – robobobobo Dec 31 '13 at 12:58
  • Can we see your entire .htaccess file? Please obscure any sensitive information, such as the actual domain name. What happens when you give it a www.domain.com domain? Do you have any other .htaccess files (in subdirectories) that might be undoing what you're doing here (e.g., convert domain.com back into www.domain.com)? – Phil Perry Dec 31 '13 at 17:29
  • I have now added the entire htaccess file as requested, I tried changing the url to something else in the htaccess file and it sill had no effect, it's almost as if it's not listening to it!The htaccess file is in the root directory, but I couldn't see any others in the sub directories that would over write it. – robobobobo Jan 01 '14 at 14:02
  • Uh, this _is_ on an Apache server, isn't it? IIS/Windows Server will ignore .htaccess files. No 500/Internal Server errors that being suppressed? Can you tell what part(s) of your .htaccess are working, such as www.domain.com -> domain.com, and narrow down the error? Try commenting out # sections and try one at a time, e.g., just the www.domain.com to domain.com. BTW, if you have other domain names being handled here, you might want to fully specify the www version rather than just www\.. The first thing you need to do is prove you're invoking .htaccess. – Phil Perry Jan 02 '14 at 14:13
  • Yep it's an Ubuntu based server running Apache. I've proven that .htaccess file works, by taking out the wordpress bits that make the pretty url's work, when I remove them, the pretty permalinks no longer work, so the .htaccess is definitely being invoked as far as I can tell, and mod rewrite is enabled and seems to be working too, just not for my www redirect! – robobobobo Jan 02 '14 at 20:50
  • did you find solution. I have same problem @robobobobo – hakki May 20 '14 at 01:59
  • No in the end I concluded that it was my servers configuration , as soon as I moved to another there was no problems – robobobobo May 20 '14 at 06:38