TYPO3 Force HTTPS in FE - .htaccess

I have two domains pointing to the same website, domain A and domain B. I want:
Domain A to be accessible via HTTP (works fine out of the box)
Domain B to redirect all requests to HTTPS. Basically if you type in http://domainb.com/somepage to redirect to http*s*://domainb.com/somepage. I would prefer to have this done via htaccess file.
I tried many solutions and I always get into redirection loop. Eg. I tried this:
RewriteCond %{SERVER_NAME} ^www\.domainb\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
....
rest of the standard typo3 htaccess commands.
My code was after typo3 be redirection part and before "Main URL rewriting" part in the htaccess file.
With code above I'm redirected to https but then "redirection loop" error is shown.
Any help is highly appreciated.

This works for me :)
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} domainb\.com$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
I include this code right after this line with RewriteEngine On.

Try this:
RewriteRule ^http://www\.domainb\.com/(.*) https://www\.domainb\.com/$1 [R=301,L]

The only solution that worked for me was the following one:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

Related

Htaccess redirect to https except ads.txt

I would like to redirect all traffic on my site to HTTPS except for the ads.txt file. Note it's to a file e.g. domain.com/ads.txt and not a sub-folder such as domain.com/all-files-in-here/
I have tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/ads.txt
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]
This isn't working according to the testing tool at https://adstxt.adnxs.com
Could someone tell me what I am doing wrong please?
Kind regards
James
I know this is an old post but I was unable to find an acceptable answer for what Google Adsense require.
"Make an ads.txt file reachable via both HTTP and HTTPS"
This works for me...
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/ads\.txt
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
When the protocol is HTTP you want to convert every url to HTTPS except "/ads.txt".
When the protocol is HTTPS you dont need to convert any url.
This provides the required outcome to have ads.txt load on both HTTP and HTTPS.
Cheers
Greg J

how to redirect subdomain to actual website

My host automatically sets up subdomains for all our hosted websites. So that zzz-thewebsite.myhosting.com is the same files as www.thewebsite.com ...
Unfortunately, somehow google has indexed the subdomains and now I probably have duplicate content.
I'd like to remove the subdomains from the index. I'm not sure the best way to do it.
I was thinking a .htaccess file that redirects zzz-thewebsite.myhosting.com to www.thewebsite.com would probably do the trick. Of course, there are subfolders involved as well.
Is there an elegant solution for this? I suppose a robots.txt might also do it but that will be in the both the zzz-thewebsite.myhosting.com and www.thewebsite.com "sites" since they are the same physical folder on the hosting.
Thank you.
Ben's answer below works great for me on non https sites but is not working for an https site. What I am using is this:
#attempt to redirect subdomain
RewriteCond %{HTTP_HOST} ^thewebsite-zzz.myhosting.com$ [NC]
RewriteRule ^(.*)$ https://www.thewebsite.com%{REQUEST_URI} [R=301,NC,L,QSA]
#https only
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#www only
RewriteCond %{HTTP_HOST} ^thewebsite.com [NC]
RewriteRule ^(.*)$ https://www.thewebsite.com/$1 [L,R=301,NC]
Setting up a permanent redirect is a preferred way.
RewriteCond %{HTTP_HOST} ^zzz-thewebsite.myhosting.com$ [NC]
RewriteRule ^(.*)$ https://www.thewebsite.com%{REQUEST_URI} [R=301,NC,L,QSA]

.htaccess : http to https redirection does not work

I have a problem with my .htaccess and https redirection.
First of all, I have consulted these links to find a solution but none could help me.
List of links :
.htaccess redirect http to https
ModRewrite with HTTPS
https://www.ndchost.com/wiki/apache/redirect-http-to-https
htaccess redirect to https://www
https://serverfault.com/questions/116206/how-do-i-use-htaccess-to-always-redirect-from-http-to-https
When I load the page with http, the .htaccess redirect me to https but when I load the page with https, I have an infinite loop.
Here is my .htaccess code :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^(api|www|dev|stefano|sav)\.
RewriteCond %{REQUEST_URI} !^/(api|www|dev|stefano|sav)/
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^(.*)$ %1/$1 [L]
Is anybody can help me create a redirect condition (http to https) ?
You are leaving off the Rewrite Flags You need to tell it force the redirection with R flag and optional provide status code which is recommended 301, 302 etc.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
By the way, every example you linked to shows the answer using the R flag, not sure why you didn't use exact examples :)
Solution if behind devices like load balancer.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have been fighting with this same problem for hours. It seems that I needed the solution for systems behind a load balancer. Perhaps this is because I am using Cloudflare with my site.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
After adding the above lines to my .htaccess file, I deleted the cache on Cloudflare and the site loads as it should.

Combining htaccess redirects and rewrites (non-www to www, http to https, directory with keyword to root)

I (sometimes) somehow manage .htaccess file successfully but need help on this one.
I'd like to achieve the following at the same time
Redirect anything non-www to www
Redirect anything with /site or /site/ to root
Redirect anything http to https
In the end, every link, no matter how it originated, should begin with https://www.domain.com/ and end with /original_request_directory_or_file_if_any
I can get #1, no problem
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/" [R=301,L]
I can get #2 no problem and it works together with #1
RedirectMatch 301 ^/site/$ http://www.domain.com/
What I am unable to achieve so far is #3
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
When I enable the above code, I get browser error for redirect loop errors.
Looking forward to a solution (because I am sure it's possible and I just don't know how.)
Thank you all in advance!
If you redirect all to https, avoid to redirect before to http.
RewriteEngine on
RewriteRule ^site/? https://www.domain.com/ [R=302,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=302,L]
Change [R=302,L] to [R=301,L] when everything works well

htaccess redirect https to http and .com to .co.uk and http://www. to http://

I have a .htaccess file set up to redirect any of the following:
http://mydomain.com
http://www.mydomain.com
http://mydomain.co.uk
to:
http://www.mydomain.co.uk
The code in the .htaccess is shown below (I realise the first line is nothing to with the redirect. I've left it in to show the code exactly as it is).
suPHP_ConfigPath /data01/xxxxx/public_html/php.ini
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
This code works perfectly, but now the client has thrown another variable into the mix. They also want to redirect the following:
https://
to
http://
I'm not sure how to implement this. I've searched for similar examples, but can't find an example that combines all the redirects that I need.
Hope this all makes sense! It's the first time I've posted a question, so I hope I've done it correctly.
Any help much appreciated :)
You can have rule this way:
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]

Resources