mpmurph
By:
mpmurph

Trying to set up redirect from non-www to www address - keep getting 'Apache2 Ubuntu Default Page.'

January 20, 2015 2.3k views

Hello all -

I am new to web development and need some help!

I understand that for SEO purposes, it is a good idea to have one's site either direct from the non-www to the www version of the domain name or vice versa. I have decided I would like my non-www version to redirect to the www version.

In my .htaccess file I have the following code (only the website name has been changed to "example"):
**
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Redirect to one domain...
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

</IfModule>**

This code was all default code with the exception of the "# Redirect to one domain..." section.

In my 000-default.conf file, I have the following line of code between the <Directory> </Directory> tags:

AllowOverride All

When I enter "a2enmod rewrite" on the command line, I am told "Module rewrite already enabled." I have also entered the line "service apache2 restart" successfully.

All this said, when I go to example.com, I am just getting the 'Apache2 Ubuntu Default Page' - on Firefox, Chrome and Safari. I have tried re-ordering my code, fiddling with the regex (some sites have the escape slash, some don't, some don't have '$' in the RewriteCond, others do)... but nothing seems to make the redirect work.

I am really confused at this point as to why example.com is not redirecting to www.example.com. Do I need to add a ServerAlias line in my default.conf file? I understood that was something different - that it wouldn't change example.com to www.example.com in the user's browser... And that it wouldn't help with the SEO problem - or am I wrong? I am running out of ideas...

Any advice/guidance would be greatly appreciated!!

Thank you!

2 comments
  • try:

    
    # Redirect to one domain...
    RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
    RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
    
  • Hi there! Thanks for the quick response! I tried the code you outlined but I am afraid I still get the 'Apache2 Ubuntu Default Page' when I go to example.com - and, when I type www.example.com, it now redirects to www.www.example.com.

    I also just tried the following with exactly the same result...

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

    This seems to imply to me that the .htaccess file is being read when I go to www.example.com but not when I go to example.com.... Is this where having an alias comes in? Should I be implementing one in my default.conf file (as below)?

    <VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot "/var/www/html/example/public"
    <Directory "/var/www/html/example/public">
    AllowOverride All
    </Directory>
    </VirtualHost>

    Or, should I be creating a separate ServerName listing in default.conf for example.com (so effectively have the following code)?

    <VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot "/var/www/html/example/public"
    <Directory "/var/www/html/example/public">
    AllowOverride All
    </Directory>
    </VirtualHost>

    <VirtualHost *:80>
    ServerName example.com
    DocumentRoot "/var/www/html/example/public"
    <Directory "/var/www/html/example/public">
    AllowOverride All
    </Directory>
    </VirtualHost>

    Thanks for the help!

1 Answer

I seem to have gotten it to work with the implementation of the line "ServerAlias example.com" in my 000-default.conf file and then rebooting!

Have another answer? Share your knowledge.