I am trying to redirect all traffic from http/https www to https only non-www. I have got http to https setup using the following on apache 2.4:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://*%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
It also needs to work inside of the virtual hosts conf file and not .htaccess as my cms could overwrite the .htaccess on upgrades. I want to add www redirect as my site may have users that are used to typing www. Thanks for all of your help in advance.
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!
hello i have a problem, i want to redirect to non www but its not working when i use https://www it just works with http://www. or with http://domain those last to redirect correctly to https://domain but not when i add www and https, why is that?
Hey there,
Here is a great tutorial that outlines redirecting www subdomains to non-www URL’s.
Here’s an example of the rewrite rule that should work and still forward to https:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Happy coding,
Jon Schwenn Platform Support Specialist DigitalOcean
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.