RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I'm trying force everything to https and eliminate www. I know this has been hashed over and over - but I'm trying to understand why this version - in particular - is not working. When I add the above code to htaccess, the force https works great - but it does not remove the www. How is that even possible given the explicit url that I'm using in the rewriterule?? I've tried removing cache, different browser, different machine, etc. Could there be something else at the server level overriding it? tx
You can use this rule to do both i.e. http->https and remove www:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
Make sure to keep this rule as first rule and clear your browser cache.
Related
http://www. version of my doesn't get redirected to https version (all other variants are properly redirected)
Website url:
http://www.alfarestents.com/
https://www.alfarestents.com/
Htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS} ^on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteCond %{HTTP_HOST} ^alfarestents.com [NC]
RewriteRule ^(.*)$ https://www.alfarestents.com/$1 [L,R=301]
Let's start this from scratch:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,R=301,END]
It probably is a good idea to start with a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as expected.
You should implement such rules in the real http server's host configuration. You can also use a distributed configuration file, but that comes with a performance penalty. Also you need to enable the consideration of such files first.
I have an .htaccess file that sends example1.com, example2.com, and example3.com to different files on my server
I want to make it so example3.com temporarily redirects to a subdomain that has been given its own A record: forums.example3.com. Would like to do it in .htaccess instead of DNS settings. It should show forums.example3.com in the URL bar too. I want to also make it so I can remove the redirect in the future without too much re-coding. So far i cant even figure out the redirect as every attempt at rewriting this working code for the redirect always fails:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([^.]+.)example2.com [NC]
RewriteCond %{HTTP_HOST} !^example2.com$
RewriteRule ^(.)$ http://example2.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([^.]+.)example3.com [NC]
RewriteCond %{HTTP_HOST} !^example3.com$
RewriteRule ^(.)$ http://example3.com/$1 [R=301,L]
#othersites in othersites folder
RewriteCond $1 !^othersites/
RewriteCond %{HTTP_HOST} ^example2.com$
RewriteRule ^(.)$ /othersites/example2.com/$1 [L]
RewriteCond $1 !^othersites/
RewriteCond %{HTTP_HOST} ^example3.com$
RewriteRule ^(.)$ /othersites/example3.com/$1 [L]
what code should I add/change to redirect example3.com to forums.example3.com? Preferably in the most SEO friendly way.
First of all, this rule is most likely wrong: RewriteRule ^(.)$
One single dot represents one single character, whereas you usually want to allow zero or more.
What you want instead should be RewriteRule ^(.*)$
The exclamation mark specifies a negative match. So if the host being requested is example3.com, then this rule will be met:
RewriteCond %{HTTP_HOST} !^example.com$
But the previous one would fail anyway:
RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]
However I would write more explicit rules and avoid negative matches if unnecessary, as they can easily lead to unwanted results.
So, the following rule should be enough to redirect example3.com (with or without subdomain) to http://forums.example3.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} (.+\.)?example3.com$ [NC]
RewriteRule ^(.)$ http://forums.example3.com/$1 [R=302,L]
Since you are saying you want to set up a temporary redirect, you may instead use HTTP code 302 for the redirect.
Looking to redirect an entire domain to its https://example.com version for any variants that are requested (http://, https://www., and www.) in the most SEO friendly way possible.
(I am aware that there are a lot of similar Q/A's, but they seem to be specific so I decided to start with a clean post).
I am currently using this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
But I have been told that the redirect is not 301, and it also does not redirect the https://www. version.
Is there a simple way to do this all in one go?
Thank you in advance!
Take care
You can use a single rule in your site root .htaccess for this:
RewriteEngine On
# remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Use a new browser to test the change.
Hi I'm trying to do the following.
When the domain is accessed through http it should redirect to https.
When domain contains www it should redirect to https
When domain contains language abbreviation it should remove it.
Possible urls:
1: http://www.example.com/en/how-did-it-all-start
2: http://example.com/en/how-did-it-all-start
3: http://www.example.com/how-did-it-all-start
These domains should redirect with one 301 redirect to
https://example.com/how-did-it-all-start
I have tried numerous things but nothing seams to work when I'm trying to combine it with the www. It now only works for http urls, when I add the www to it, it breaks.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} ^en/(.*)$ [NC]
RewriteRule ^ https://%1%/%2% [R=301,L]
I don't know how to properly combine those to conditions without breaking it.
Tried to do in different ways but until now no success.
Any help would be appreciated.
Thanks in advance.
You need to have [OR] clauses in your conditional with optional matches to handle all 3 cases:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:en/)?(.*)$ https://%1/$1 [R=301,L,NE]
Currently using the following to make sure my website always is using www. How would I modify this to be www and https?
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Tried this, but ended up with www.www.website if the user had no https but did already have a www
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Not sure how to combine the two
Make us of {SERVER_PORT} to see if the request is made via regular connection (80), then redirect to the https URL.
This should do it:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Here are some nice example, using different methods.