Redirect all URLs to no-www https - .htaccess

I would like to forward all requests to a domain via .htaccess.
Everything to: https://domain.tdl
No-www
https
I have tried many spelling, but it has not worked for completely.

I think the following single set of rule-set should work:
RewriteEngine On
RewriteCond %{HTTPS} off [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.tld%{REQUEST_URI} [R=301,L]

Related

htaccess redirect subfolder to root and force https

I'm trying to redirect all URLs with a subfolder to the root, but also force https and www
So, for example, I want to redirect:
example.com/shop/category/product
www.example.com/shop/category/product
example.com/SHOP/category/product
to
https://www.example.com/category/product
I've tried the following code
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
but this doesn't redirect the subfolder, and if I add the subfolder as a previous rule like
RewriteRule ^shop/(.*)$ /$1 [R=301,NC,L]
It just doesn't work.
One thing to point out - I must also make sure that any other requests to the site (with or without the subfolder) are being redirected to the https www version.
Can anybody point me in the right direction or help me solve this? TIA
You can use this single rule for this redirect at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} ^/shop/ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:shop/)?.*$ https://www.%1/$0 [R=301,L,NC,NE]
# rest of your rules go below
Make sure to clear browser cache or use a new browser for testing.

Htaccess rewrite url and remove part from url

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]

redirect permenant https://site.org.in to https://www.site.org.in on apache [duplicate]

I have this in my .htaccess file:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301,L]
but whenever I access a file on my root like http://example.com/robots.txt it will redirect to http://www.example.comrobots.txt/.
How can I correct this so that it will redirect correctly to http://www.example.com/robots.txt?
Change your configuration to this (add a slash):
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Or the solution outlined below (proposed by #absiddiqueLive) will work for any domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If you need to support http and https and preserve the protocol choice try the following:
RewriteRule ^login\$ https://www.%{HTTP_HOST}/login [R=301,L]
Where you replace login with checkout.php or whatever URL you need to support HTTPS on.
I'd argue this is a bad idea though. For the reasoning please read this answer.
Here's the correct solution which supports https and http:
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
UPD.: for domains like .co.uk, replace
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
with
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+\.[^.]+$
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
For Https
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^(.*)$ http%1://www.%{HTTP_HOST}/$1 [R=301,L]
The following example works on both ssl and non-ssl and is much faster as you use just one rule to manage http and https
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|offs()
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
[Tested]
This will redirect
http
http://example.com
to
http://www.example.com
https
https://example.com
to
https://www.example.com
Try this, I used it in many websites, it works perfectly
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^bewebdeveloper.com$
RewriteRule ^(.*) http://www.bewebdeveloper.com/$1 [QSA,L,R=301]
I have tested all the above solutions but not working for me, i have tried to remove the http:// and won't redirect also removed the www it redirect well, so i get confused, specially i am running all my sites under https://
So i have combined some codes together and came up with perfect solution for both http:// and https:// and www and non-www.
# HTTPS forced
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# Redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
Hope this can help someone :)
Add the following code in .htaccess file.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
URLs redirect tutorial can be found from here - Redirect non-www to www & HTTP to HTTPS using .htaccess file
This configuration worked for me in bitnami wordpress with SSL configured :
Added the below under "RewriteEngine On" in file /opt/bitnami/apps/wordpress/conf/httpd-app.conf
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]
This will redirect your domain which is not started with WWW
It is not redirect your all sub domain.
It is useful.
I believe the top answer successfully redirects non-www to www (ex: mysite.com -> www.mysite.com), but doesn't take into account wildcard subdomains, which results in:
random.mysite.com -> www.random.mysite.com
Here's a solution with/without HTTPS
HTTP
RewriteEngine On
RewriteCond %{HTTP_HOST} !www.mysite.com$ [NC]
RewriteRule ^(.*)$ http%{ENV:protossl}://www.mysite.com/$1 [L,R=301]
HTTP/HTTPS
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=protocol:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=protocol:http]
RewriteCond %{HTTP_HOST} !www.mysite.com$ [NC]
RewriteRule ^(.*)$ %{ENV:protocol}://www.mysite.com/$1 [L,R=301]
*note: I haven't tested https because I don't currently have a cert to test, but if someone can verify or optimize what I have that would be awesome.
Two warnings
Avoid 301 and prefer modern 303 or 307 response status codes.
Avoid 301
Think carefully if you really need the permanent redirect indicated as [R=301] because if you decide to change it later, then the previous visitors of the page will continue to see the page of the original redirection.
The permanent redirection information is frequently stored in the browser's cache and, in general, it is difficult to eliminate (reload the page do not solve the problem). Your website visitors will be stuck in the previous redirect "forever".
Avoid 302 too
The new version of the HTTP protocol (v1.1) added two new response status codes that can be used instead of 302.
303 URL redirection but demanding to change the type of request to
GET.
307 URL Redirection but demanding to keep the type of request as initially sent.
You can still use the code 302 (non-permanent redirection) although it is considered ambiguous. In any case, most browsers implement 302 in the same way the new 303 code instructs.
If possible, add this to the main Apache configuration file. It is a lighter-weight solution, less processing required.
<VirtualHost 64.65.66.67>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost 64.65.66.67>
ServerAdmin me#example.com
ServerName www.example.com
DocumentRoot /var/www/example
.
.
. etc
So, the separate VirtualHost for "example.com" captures those requests and then permanently redirects them to your main VirtualHost. So there's no REGEX parsing with every request, and your client browsers will cache the redirect so they'll never (or rarely) request the "wrong" url again, saving you on server load.
Note, the trailing slash in Redirect permanent / http://www.example.com/.
Without it, a redirect from example.com/asdf would redirect to http://www.example.comasdf instead of http://www.example.com/asdf.
Write in .htaccess :)
## Redirect from non-www to www (remove the two lines below to enable)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

.htaccess code not adding automatic www & https before the url

This is my .htaccess code:
# RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
I had added this code to add www & https in the URL forcefully if it is not added. This code worked fine up to some hours ago, but as I tweaked my .htaccess code this code won't work.
In Chrome, Mozilla browsers it's not adding https://www. as a prefix to the URL, it doesn't contain www. or https://www. or http://
I have even reset the cache and data of the Mozilla, chrome browser but the same thing is happening again and again.
Any helps appreciated. Thanks in advance.
To enforce www and https in same rule use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Make sure you clear your browser cache completely before testing this change.
PS: In case you want to do this ONLY for one specific domain then use 3rd RewriteCond as:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(hashstar\.com)$ [NC]

Redirect from non-www to www and force SSL

I'd like to force all http traffic to https, and also always force www.
This is what I have so far:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
This seems to correctly work when the url does not contain www. So http://mydomain.com correctly redirects to https://www.mydomain.com.
However, it is not correctly redirecting to https when the www part is present. So, www.mydomain.com is not redirecting to https://www.mydomain.com
Edit
I have got this working with two rewrite blocks:
# Force ssl
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# redirect non-www to www
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
No idea if this is acceptable or not, but it works
Use your domain name and not HTTP_HOST in the rewriterule so that way it won't matter if www is there or not and you can use 1 rule. You can have two rewrite rules but just use OR. I think you were looking for is this.
# always redirect to www and/or https
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC,OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*) https://www.mydomain.com/$1 [R=301,L]

Resources