0

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

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Luke101
  • 63,072
  • 85
  • 231
  • 359
  • Why don't you just let Wordpress Permalinks handle the removal of the WWW? – Urda Mar 05 '10 at 18:21
  • when I run the website nothing happens and the www is still there www.akorra.com – Luke101 Mar 05 '10 at 18:24
  • @Felix, that article's answer doesn't apply here. That article was for ASP.NET and not via .htaccess – Urda Mar 05 '10 at 18:25
  • the code I have posted above is different from the duplicate posts. It has 2 rewriterules statements which makes it different – Luke101 Mar 05 '10 at 18:25
  • And what about this: http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www Duplicate? – Felix Kling Mar 05 '10 at 18:41
  • @Felix, The generic redirect is very nice, but I really think his Wordpress was to blame (thus making the question's answer unique). I also just went to the domain he posted (www.akorra.com) and it looks like it redirected to a non-www just fine. – Urda Mar 05 '10 at 19:02
  • @Luke101 you have any luck with your question? – Urda Mar 08 '10 at 17:15

5 Answers5

1

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.

Urda
  • 5,460
  • 5
  • 34
  • 47
1

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>
Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
  • This is not true. The IfModule only makes sure not everything gives a 500 when there is no mod_rewrite.c enabled. – Herman May 05 '13 at 07:10
0

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

Robin Layfield
  • 1,599
  • 3
  • 16
  • 14
0
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

ahmet2106
  • 4,957
  • 4
  • 27
  • 38
0

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>
Mojtaba Rezaeian
  • 8,268
  • 8
  • 31
  • 54