Forum Moderators: phranque
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L] RewriteEngine On
# Check that you're on port 443 and the hostname starts with www
RewriteCond %{SERVER_PORT} ^443
RewriteCond %{HTTP_HOST} ^www\.
# Redirect to domain without the www
RewriteRule (.*) https://example.com$1 [L,R,QSA] RewriteCond %{REQUEST_URI} !^/robots\.txt
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) https://example.com/$1 [R=301,L]
I added the RewriteCond involving robots.txt on my own HTTPS site because I noticed that some law-abiding robots seemed to get confused with a robots.txt redirect, and you should bend over backward to avoid giving them an excuse not to get this file. The alternative is to make a preliminary rule that says simply RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$ [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L] did you try something like this?Found a few similar examples, tried and I don't remember any of those working. I tried tweaking the lines, my knowledge on htaccess is limited and when I don't use it for long it becomes more difficult. I didn't perform enough tests on such code examples because I wanted to stay away from writing the domain name directly on the htaccess file. I have a custom built CMS running several sites, it was created in such way I can move the files all around and update the core easily because all the specifics are on config files. Writing specific domains on htaccess or scripts is an option, but not one that fits what I'm aiming for.
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$ [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L] also check your server access log file for these redirected requests. on some servers, a 301 generated after an internal rewrite to a script looks like a 200 response in the access log.Checked using online header tools and also checked the server logs, it looks good, the script in my case is returning a solid 301.
on some servers, a 301 generated after an internal rewrite to a script looks like a 200 response in the access log.I would say on most servers, since the server log only records that the request has been successfully handed off to the script; if the script itself physically exists, that's a 200. Similarly, a CMS-based site may never show a 404 in access logs, since it is the script's job to figure out whether the requested content exists.
Lucy24: I would say on most servers, since the server log only records that the request has been successfully handed off to the script; if the script itself physically exists, that's a 200.
Eventually the search engine willFor a given definition of “eventually”. Bing, in particular, just refuses to give up. For some reason, this particularly applies to images; to this day I’m getting redirected requests, both for images that moved to a different site in 2013, and images that went HTTPS a year ago. Generally the same few images, so it's not as if the search engine didn't know or hasn't had time to catch up.