###I’ve recently changed the config files of my virtual hosts and rather than use an .htaccess file at the directory root I wanted to use the config file for server optimization. However it’s not working.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
RewriteEngine On
# RewriteBase /
</IfModule>
####And using this rewrite condition…
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
####With the virtual host set as follows…
<virtualhost>
ServerName: example.com
ServerAlias: www.example.com
.......
</virtualhost>
####The WWW URL is still NOT redirecting to non-WWW URL.
What am I missing? Is this not possible to do using Digital Ocean? I could really use someones help. I’ve tried other methods such as update the A and CNAME records based on some of the communities suggestions but still have not been able to get this to work. Help anyone? Please and 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!
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.
When configuring Virtual Hosts you can 1. upload an .htaccess file (which is not recommended by Apache as it can slow down server) or 2. edit your virtual hosts config file directly. I took the challenge of editing the Virtual Host Server Config file directly using HTML5 Boilerplate and Google Web Toolkit recommendations.
So how did I do it you might ask…
Editing your main server config file (e.g. /etc/apache2/apache2.conf) is fairly simple but when trying to do so per Virtual Host the structure is slightly different.
In your virtual host config file (e.g. /etc/apache2/sites-available/yoursite.com.conf) your setup should look as follows:
Now, suppose you wanted to make sure that all WWW URL’s are redirected to non-WWW URL’s you would have to do the following:
For cleaner (reusable) code it may be best to declare server global directives in the your main server config file (again you can find it here: /etc/apache2/apache2.conf). However be careful when doing so as this may not be right for every project. Here is a great beginners introduction to Apache Server Config’s by Justin Ellingwood.
Side Note: I’m not an experienced Server Admin by any means and am learning while keeping best practices in mind. Should anything stated in response that is not accurate or not a good practice please comment. Hoping this might spark some general discussion among Server Ninjas & Novices on Server Best Practices for optimal performance on Digital Ocean.
Have you checked in which virtualhost definition your rewrites reside? I you the virtualhosts separated between port 80 and port 443, it might go wrong
<virtualhost *:80=“”>
vs
<virtualhost *:443=“”>
if you try to rewrite from https://www to https:// (without www) in the virtualhost listening on port 80, it won’t work. Because it will never get there (it’s not https there). That rewrite rule should be in the virtualhost listening on port 443 - or make sure your virtualhost listens to any port, of course.