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

  • Posted on January 20, 2015
  • mpmurphAsked by mpmurph

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} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]


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, 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 is not redirecting to Do I need to add a ServerAlias line in my default.conf file? I understood that was something different - that it wouldn’t change to 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!

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Rather than changing the 000-default.conf file you can edit your /etc/apache2/sites-enabled/<my web app>.conf file and change the ServerName from an ip address to <my domain name>.com

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