I’m running Ubuntu 14.04 incl. SSL certificate via Serverpilot. I’m looking for a way to redirect all website traffic in the following order:
all http --> https all www --> non-www
Here’s what I have in my htaccess file:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
This works in Firefox but in Chrome & Safari (incl. iOS) it doesn’t redirect to https if I call my website via “non-ssl / non-www”.
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!
An update on an older topic in case anyone stumbles upon it
To redirect non-www to www URLs using both Nginx or Apache, you can use the following configurations. First, we’ll provide the Nginx example, and then the Apache example using .htaccess
rules.
Nginx Configuration:
Open your Nginx site configuration file in a text editor. This file is usually located in /etc/nginx/sites-available/
or /etc/nginx/conf.d/
and typically has a .conf
extension, like example.com.conf
.
Add the following server block configuration to redirect non-www to www:
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
# Your existing Nginx configuration for www.example.com goes here
}
Replace example.com
with your actual domain name. This configuration listens for requests to example.com
and redirects them to www.example.com
using a 301 (permanent) redirect.
Save the file and exit the text editor.
Test the Nginx configuration to ensure there are no syntax errors:
sudo nginx -t
sudo systemctl reload nginx
Apache Configuration using .htaccess:
Create an .htaccess
file in the root directory of your website if it doesn’t already exist.
Add the following code to your .htaccess
file to redirect non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Replace `example.com` with your actual domain name.
Save the .htaccess
file.
Test your Apache configuration by accessing your website in a web browser without the www prefix (e.g., http://example.com
). It should automatically redirect to the www version (e.g., http://www.example.com
).
Both the Nginx and Apache configurations provided above will redirect visitors from non-www URLs to www URLs. Make sure to adjust the domain name accordingly in the configuration examples.
I use .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com%{REQUEST_URI} [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule (.*) https://example.com%{REQUEST_URI} [L,R=301,NC]
This comment has been deleted
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.