<p>The answer to this question was the positioning of content in the .htaccess file.</p>

<p>Move:</p>

<pre><code>RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</code></pre>

<p>Before this:</p>

<pre><code># 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]
</code></pre>

<p>Although the L tag was in the previous statement, it should be the last rule applied, it doesn't work (probably because I was activating the RewriteEngine again later so it resets the rules, but if the L flag was active in the first rule, it wouldn't apply the www one anyway).</p>

<p>The rule of thumb is: first do your cosmetic changes to the domain, then apply the functional rules.</p>

CC BY-SA 3.0
2017-01-10T09:35:20.563Z
2017-01-10T09:35:20.563Z
zizther
<p>Check out the official Apache documentation on Canonical domain rewrites: <a href="https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost" rel="nofollow noreferrer">https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost</a></p>

CC BY-SA 3.0
2017-01-09T23:00:06.140Z
Thanks. I have had a look through these, none of the changes I have made has fixed the issue.
I am hosting the site on Digital Ocean, not sure if that affects anything in the vhost or htaccess setup
zizther
CC BY-SA 3.0
2017-01-09T01:03:32.350Z
2017-01-09T01:03:32.350Z
andrew.welch
<p>I am using htaccess to rewrite any www url to a non-www version</p>

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

<p>When I type <code>domain.com/about</code> it works fine, however when I type <code>www.domain.com/about</code> it changes the URL to <code>domain.com/index.php?p=about</code></p>

<p>I have <code>'omitScriptNameInUrls' =&gt; true,</code> enabled.
I also have this in my .htaccess file</p>

<pre><code>RewriteEngine On

# 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]
</code></pre>

<p>What am I doing wrong here?</p>

<p>Edit:
Here is the the vhost file.</p>

<pre><code>&lt;VirtualHost *:80&gt;
    ServerName domain.com
    ServerAdmin webmaster@domain.com
    DocumentRoot /var/www/

    &lt;Directory /var/www/&gt;
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    &lt;/Directory&gt;

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
&lt;/VirtualHost&gt;
</code></pre>

CC BY-SA 3.0
2017-01-09T00:46:57.653Z
Any of these help? https://craftcms.com/support/remove-index.php https://craftcms.com/support/why-index.php
Brad Bell
CC BY-SA 3.0
2017-01-09T00:51:18.680Z
@BradBell Thanks. I do have both of those in my .htaccess files and config already. I have edited my question to reflect that.
zizther
<url><redirect><htaccess>
htaccess non-www rewrite showing index.php in URL
# htaccess non-www rewrite showing index.php in URL

(Asked by: username_0 on 2017-01-09)

I am using htaccess to rewrite any www url to a non-www version
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

When I type domain.com/about it works fine, however when I type www.domain.com/about it changes the URL to domain.com/index.php?p=about
I have 'omitScriptNameInUrls' => true, enabled.
I also have this in my .htaccess file
RewriteEngine On

# 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]

What am I doing wrong here?
Edit:
Here is the the vhost file.

    ServerName domain.com
    ServerAdmin webmaster@domain.com
    DocumentRoot /var/www/

    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

**Tags:** url, redirect, htaccess

> **Comments:**
> **username_1**: Any of these help? https://craftcms.com/support/remove-index.php https://craftcms.com/support/why-index.php  
> **username_0**: @BradBell Thanks. I do have both of those in my .htaccess files and config already. I have edited my question to reflect that.  

---

## 2 answers

### Answer by username_0 on 2017-01-10. Score: 1

The answer to this question was the positioning of content in the .htaccess file.
Move:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]

Before this:
# 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]

Although the L tag was in the previous statement, it should be the last rule applied, it doesn't work (probably because I was activating the RewriteEngine again later so it resets the rules, but if the L flag was active in the first rule, it wouldn't apply the www one anyway).
The rule of thumb is: first do your cosmetic changes to the domain, then apply the functional rules.


---

### Answer by username_2 on 2017-01-09. Score: 1

Check out the official Apache documentation on Canonical domain rewrites: <https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost>

> **Comments:**
> **username_0**: Thanks. I have had a look through these, none of the changes I have made has fixed the issue. I am hosting the site on Digital Ocean, not sure if that affects anything in the vhost or htaccess setup  

---

