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!
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.
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, likeexample.com.conf
.Add the following server block configuration to redirect non-www to www:
Replace
example.com
with your actual domain name. This configuration listens for requests toexample.com
and redirects them towww.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:
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: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
This comment has been deleted