0

I need all non-www requests to redirect to www. I believe I'm doing this right, but this code is not working:

<IfModule mod_rewrite.c>

Options +FollowSymlinks
RewriteEngine on  

# If www is missing from the beginning of the URL
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

</IfModule>

Any ideas what could be wrong here?

UPDATE:

I thought the answer was here: http://www.jarrodoberto.com/articles/2011/11/enabling-mod-rewrite-on-ubuntu

I'm on Ubuntu and my default site did not have AllowOverride to to All in areas shown below:

<Directory />
    Options FollowSymLinks
    AllowOverride All                                 <- HERE
</Directory>
<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All                                 <- HERE
    Order allow,deny
    allow from all
</Directory>

I was hoping that after an Apache restart the problem would be resolved, but I'm still not seeing a redirect.

0

THE SOLUTION

It seems that the problem was a mis-configuration in my vhost setup file:

This is wrong. Notice that the ServerName is set to www.mysite.com and the ServerAlias is set to www-prod.mysite.com:

<VirtualHost *:80>
    ServerName www.mysite.com
    ServerAlias www-prod.mysite.com
    DocumentRoot /var/www/vhosts/.../html_www
    <Directory /var/www/vhosts/.../html_www>
        AllowOverride All
    </Directory>
    CustomLog /var/log/apache2/...-access_log common
    ErrorLog /var/log/apache2/...-error_log
</VirtualHost>

This is right. The ServerName and ServerAlias need to be set as such:

<VirtualHost *:80>
    ServerName mysite.com
    ServerAlias www.mysite.com
    DocumentRoot /var/www/vhosts/.../html_www
    <Directory /var/www/vhosts/.../html_www>
        AllowOverride All
    </Directory>
    CustomLog /var/log/apache2/...-access_log common
    ErrorLog /var/log/apache2/...-error_log
</VirtualHost>

In my defense, this was not configured by me. I think our other developer was attempting to do something tricky with the server alias.

1
  • Actually, looking at this, should these be the other way around? ServerName = www.mysite.com and ServerAlias = mysite.com? – Vaughn D. Taylor Aug 1 '15 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.