<p>Providing you have the necessary permissions to do this, the following in your .htaccess should do the job:</p>

<pre><code>RewriteCond %{HTTP_HOST} !^domain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://domain.co.uk/$1 [R=301,L]
</code></pre>

<p>In place, the full file will probably look something like this:</p>

<pre><code>&lt;IfModule mod_rewrite.c&gt;
RewriteEngine On

# Redirect to non-www domain
RewriteCond %{HTTP_HOST} !^domain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://domain.co.uk/$1 [R=301,L]

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
&lt;/IfModule&gt;
</code></pre>

<p>The following seems to work for me as a generic option:</p>

<pre><code>&lt;IfModule mod_rewrite.c&gt;
RewriteEngine On

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

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

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
&lt;/IfModule&gt;
</code></pre>

CC BY-SA 3.0
2015-01-08T15:03:05.660Z
That seems to work, thanks Steve. Any idea how I might do it generically without using the domain name?
darylknight
CC BY-SA 3.0
2015-01-08T15:19:15.013Z
I've updated the above with some generic code (taken from your original post) that seems to work for me without error. Can you give it a shot and see?
Steve Abraham
CC BY-SA 3.0
2015-01-08T16:15:08.937Z
Seems to be fine, thanks muchly! http://www.codeknight.co.uk/portfolio
darylknight
CC BY-SA 3.0
2015-01-08T17:49:01.523Z
If you are using `.htaccess` for the redirect, the redirect happens before `index.php` is called (and therefore also before `config.php` is called.)
Marion Newlevant
CC BY-SA 3.0
2015-01-08T13:13:35.867Z
2015-01-08T15:18:41.197Z
2015-01-08T15:18:41.197Z
Steve Abraham
<p>I need to set up a redirect from the www version of my site to the non-www version for SEO. I found a solution that <em>would</em> work <a href="https://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www#answer-1270281">here</a> if it weren't for the fact that it breaks the index.php redirect that comes with craft.</p>

<p>Can anyone tell me how to implement a generic redirect from <a href="http://www.site.co.uk" rel="nofollow noreferrer">http://www.site.co.uk</a> to <a href="http://site.co.uk" rel="nofollow noreferrer">http://site.co.uk</a> and still keep the index.php craft redirect working?</p>

<p>Additionally, does this affect the config.php file, or does that get loaded AFTER the redirect?</p>

<p>Thanks!</p>

<redirect><htaccess><index>
htaccess www to non-www redirect (and config.php)
# htaccess www to non-www redirect (and config.php)

(Asked by: username_0 on 2015-01-08)

I need to set up a redirect from the www version of my site to the non-www version for SEO. I found a solution that *would* work [here](https://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www#answer-1270281) if it weren't for the fact that it breaks the index.php redirect that comes with craft.

Can anyone tell me how to implement a generic redirect from <http://www.site.co.uk> to <http://site.co.uk> and still keep the index.php craft redirect working?

Additionally, does this affect the config.php file, or does that get loaded AFTER the redirect?

Thanks!

**Tags:** redirect, htaccess, index


---

## 1 answers

### Answer by username_1 on 2015-01-08. Score: 6 | Accepted answer

Providing you have the necessary permissions to do this, the following in your .htaccess should do the job:
RewriteCond %{HTTP_HOST} !^domain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://domain.co.uk/$1 [R=301,L]

In place, the full file will probably look something like this:

RewriteEngine On

# Redirect to non-www domain
RewriteCond %{HTTP_HOST} !^domain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://domain.co.uk/$1 [R=301,L]

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]


The following seems to work for me as a generic option:

RewriteEngine On

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

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

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]

> **Comments:**
> **username_0**: That seems to work, thanks Steve. Any idea how I might do it generically without using the domain name?  
> **username_1**: I've updated the above with some generic code (taken from your original post) that seems to work for me without error. Can you give it a shot and see?  
> **username_0**: Seems to be fine, thanks muchly! http://www.codeknight.co.uk/portfolio  
> **username_2**: If you are using `.htaccess` for the redirect, the redirect happens before `index.php` is called (and therefore also before `config.php` is called.)  

---

