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} ^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!
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!
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 example.com” in my 000-default.conf file and then rebooting!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.