Htaccess redirect to https except ads.txt - .htaccess

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

Related

how to allow ec2 health check through .htaccess while redirecting to https

I'm trying to create an .htaccess file that does the following:
1 Allows the EC2 health check URL to be hit without redirecting to https
2 Redirects all non-https traffic to https
3 Redirect calls to / to /auth/app/public/app (using https)
Items 2 and 3 work fine, but quickly the healthcheck fails and the server no longer responds. Here's the content of my .htaccess file:
RewriteEngine On
#one attempt that didn't work
#RewriteCond %{HTTP_HOST} !^my\.domain\.com$ [NC]
#RewriteCond %{REQUEST_URI} !^/healthcheck.html$
#RewriteRule ^ http://my.domain.com/$1 [R=301,L]
#another attempt that didn't work
#RewriteCond %{HTTP_HOST} !$ [NC]
#RewriteCond %{REQUEST_URI} !^/healthcheck.html$
#RewriteRule ^(.*)$ /$1 [L,QSA]
#this works
RewriteRule ^$ https://my.domain.com/auth/app/public/app/ [L]
#this works
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://my.domain.com%{REQUEST_URI} [L]
The first two commented-out attempts are from examples I found at https://serverfault.com/questions/470015/how-should-i-configure-my-elb-health-check-when-using-namevirtualhosts-and-redir
and https://serverfault.com/questions/470015/how-should-i-configure-my-elb-health-check-when-using-namevirtualhosts-and-redir/597541#597541
Please let me know if you have any suggestions how I can get this working.
Thank you for the reply. Before I read that, I found another post that provided a solution that is working for me. I simply added another condition to not apply the https redirect rule if the url was that of my health check page. Here is the working version of my .htaccess file:
RewriteEngine On
RewriteRule ^$ https://my.domain.com/auth/app/public/app/ [L]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/healthcheck\.html$
RewriteRule ^ https://my.domain.com%{REQUEST_URI} [L]

.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.

TYPO3 Force HTTPS in FE

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]

Is it possible to have a certain files in a server to redirect to https and others are forced to only http no matter if you put https

Is it possible to do this on .htaccess
if(filename.php = "order.php"){
redirect to https
}else{
redirect to http
}
I need it to do something like this, user might want to put https on my index.php and its unsecure and gives an error, that why I need to force to http if its not order.php, and force https if its order.php
I have 3 similar folders like in 1 server,
I tried to use this
RewriteEngine On
RewriteRule /(order.php) https://%{SERVER_NAME}%{REQUEST_URI} [L]
but failed, I did it on PHP, but I will have to put all of it hundred files.
So Iguess .htaccess will be more time efficient,
Yes it's possible
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/order.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/order.php
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

https to http in .htaccess with exceptions

I've done enough research to figure out that to redirect sitewide all of my https pages to their http equivalent, I need to use this code...
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
My issue is, there are a handle of pages that I still want to remain as https. For the sake of example, let's say I want page1.php, page2.php, and page3.php to REMAIN as .https, with everything else on the site redirecting. Anyone know how to do this?
Try something like this:
RewriteEngine On
# Force page1,2,3.php onto HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(page1|page2|page3)\.php https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
# Redirect other HTTPS requests to plain HTTP
RewriteCond %{HTTPS} on
RewriteCond ${REQUEST_URI} !(page1|page2|page3)\.php
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

Resources