Report this

What is the reason for this report?

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

Posted on January 20, 2015

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!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.