How to make www redirect to non-www links in htaccess

When you need to redirect all requests from a www domain over to your non-www domain, it needs to be done on the .htaccess file.

You can use this www-to-nonwww redirect generator to create the code.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule (.*) https://domain.com/$1 [R=301,L]
</IfModule>

Siteground makes www redirects easy

If you happen to be a Siteground customer, they have a great tool to make this happen. Head to your Siteground dashboard > Site Tools > SITE > File Manager.

Go to the public_html folder and right-click on .htaccess; choose Edit. Don’t forget to save your edited file!

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

How to redirect www URLs to non-www?
To redirect all requests to non-www, add the following lines at the beginning of your website’s .htaccess file: Replace yourdomain.com with your actual dom

How Do I Set Up a Single Page Redirect?

Aleyda also has an awesome page-to-page 301 redirect generator that will automatically create the code you need to use in an .htaccess file.

The process is straight forward too. For the first step, enter your old url. This is a relative url which doesn’t include your domain name. i.e. /old-blog-post

/old-blog-post

Then, put in the new link that you will be redirecting users to. Also a relative url.

/new-blog-post

Look in the last box that says, “Copy this Rule in to .htaccess file”. Take this exactly as you read it and place it in the beginning of the file.

<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 /old-blog-post /new-blog-post

</IfModule>

How to Redirect from HTTP to HTTPS

Take the following code and insert it into your .htaccess file. This will take any link with the slug and change it from an http (non-secure) to https (secure) page.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Should I use a 301 or 302 redirect?

These are really the only two options in my world. It makes things simple and since I’m a black and white thinker anyway, picking one or the other is easier to decide.

Here’s the difference between how they work.

  • a 301 redirect is seen a permanent, not going to change.
  • a 302 redirect is used for something temporary that may change in the future.