Redirect target URL misses slash after domain - .htaccess

I am trying to redirect all traffic to HTTPS and WWW.
Using this in my .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But going to my domain example.com/blog redirects to https://www.example.comblog. This does not have a slash after the domain before the subdirectory.
Any suggestions?

Related

.htaccess redirect from non-www http subfolder to www https subfolder

I use this simple code to redirect non-www http to www https
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It succesfully redirect http://example.com to https://www.example.com.
But when I tried to access http://example.com/somefolder it did not redirected. Am I missing some syntax here? or something else?
Change %{REQUEST_URI} to /$1 ( first match in rule .* )
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [L,R=301]
or modify last rule pattern:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

htaccess redirect domain to https, subdomain to https and non-www to www

I’m looking to do this:
Force the https for my main domain.
http to https://www.
http://www to https://www.
But not for subdomains
http://subdomain.domain.com to https://subdomain.domain.com
Can some one help me i can't find this
You can use that in your root .htaccess:
RewriteEngine on
# redirect to https www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
# redirect to http subdomain
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^((?!www).+\.domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
[EDIT]
RewriteEngine on
# redirect no-www to www only main domain, not with subdomain
RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
# redirect http to https all domain
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect HTTP to HTTPS excluding one sup domain and add www if needed

I want to redirect my SSL site (domain.com) with HTTPS protocol but not domain.org which is actually a sub-directory of domain.com (domain.com/domainorg/). I want this:
http://domain.com TO https://www.domain.com
http://domain.org TO http://www.domain.org
I tested my htacess on http://htaccess.mwl.be/ and it works fine but when I actually run it, I get “The page isn't redirecting properly” and it appears to be in a loop when I test all options.
This is my htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www.)?domain.org$
RewriteRule ^/?(.*)$ http://www.domain.org/$1 [L,R=301]
RewriteRule ^/?(.*) https://www.domain.com/$1 [L,R=301]
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
I am not proficient at htacess at all. What do I need to correct or change?
Why are some of your rules outside <IfModule mod_rewrite.c>? I'd put all of them inside it.
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirecting domain.com http URLs to https (any non-www will be rewritten to www as well)
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# Redirecting domain.com non-www domain.com URLs to www (any non-https will be rewritten to https as well)
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
# Redirecting domain.org non-www URLs to www (any https will be rewritten to http as well)
RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
# Redirecting domain.org https URLs to http (any non-www will be rewritten to www as well)
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]
</IfModule>
You may also force URLs like this domain.com/domainorg/* to be redirected to http://www.domain.org/* using:
RewriteCond RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond RewriteCond %{REQUEST_URI} ^\/?domainorg(\/.*)?$ [NC]
RewriteRule ^.*$ http://www.domain.org%1 [R=301,L]

htaccess exclude specific folder

for a website I need a redirect from
domain without www TO domain with www
and
http TO https
The final URL has to be always https://www.myshop.com ...
I solved this successfully with this commands:
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
The problem ist that I have to exclude a specific folder.
The URL to exclude is: http://www.mydomain.de/myfolder/filename.php
This means: If this URL is requestet there mustn't be a redirect.
Other tutorials I found did not work, sorry :-(
Thanks for any help and best regards...
You can have a skip redirect rule before these 2 redirect rules you have:
RewriteEngine On
# skip myfolder/filename.php from any redirects below
RewriteCond %{THE_REQUEST} /myfolder/filename\.php[?/\s] [NC]
RewriteRule ^ - [L]
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Maybe something like that
RewriteEngine on
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteRule ^http://www.mydomain.de/myfolder/filename.php$ - [L]
RewriteRule ^.*\filename.php$ http://www.mydomain.de/myfolder/filename.php [R=301,L]
Adding something like this as a first ruleset may help. (May want to revisit those [L,R] options though.
# check if myfolder is contained
RewriteCond %{REQUEST_URI} ^[^/]*/myfolder(/.*)?$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Configuring .htaccess for multiple domains and SSL

I have a primary domain "example.co.uk" and 6 other domains + 1 IP address pointed to the same folder root.
I would like to use HTTPS for the primary domain i.e. https://www.example.co.uk and redirect all the other domains to the homepage of this domain (avoiding SEO issues and content duplication with the example.com domain and IP address)
How do I achieve this using my current .htaccess file?
RewriteEngine on
#Redirect IP address to example.co.uk.
RewriteCond %{HTTP_HOST} ^12\.34\.567\.890
RewriteRule (.*) https://www.example.co.uk/$1 [R=301,L]
# Redirect example.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example2.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example3.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example3\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example4.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example4\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example5.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example5\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example6.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example6\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
You can reduce the number of rules by negating the condition check against the HTTP HOST. Then, you'll just need the rule to check HTTPS:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
# redirect all the other domains to the homepage
RewriteCond %{HTTP_HOST} !^www\.example.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/ [L,R=301]
In your question, you said you wanted to redirect all the other domains to the homepage of this domain, but your rules aren't doing that at all. Your rules are redirecting the request to the same request but just this domain. If you want to keep that logic, then you need to add a $1 to the end of the https://www.example.co.uk/.

Resources