Combining HTAccess rules into one - html

Hey everyone im trying to use Htaccess to redirect all pages to https:// while keeping the query string without fbclid if it's there.
IE.
https://www.example.com/?sdfuh
http://www.example.com/?sdfuh
www.example.com/?sdfuh
https://www.example.com/?sdfuh&fbclid=azduh
http://www.example.com/?sdfuh&fbclid=azduh
www.example.com/?sdfuh&fbclid=azduh
example.com/?sdfuh&fbclid=azduh
They should return https://example.com/?sdfuh
Ok so after some help Ive got to this
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [AND]
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteRule ^(.*)/?(.*)$ https://example.com/?%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [AND]
RewriteCond %{QUERY_STRING} ^(.*)
RewriteRule ^(.*)/?(.*)$ https://example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(.*)/?(.*)$ https://example.com/ [R=301,L]
RewriteCond ^(www.)?example.com$
RewriteRule ^(.*)/?(.*)$ https://example.com/ [R=301,L]
This works with:
https://www.example.com/?sdfuh
http://www.example.com/?sdfuh
www.example.com/?sdfuh
https://www.example.com/?sdfuh&fbclid=azduh
http://www.example.com/?sdfuh&fbclid=azduh
Not with
www.example.com/?sdfuh&fbclid=azduh
example.com/?sdfuh&fbclid=azduh
I need to figure out how to make it shorter and work with all the possibilities

Rather than literally combining the rules, I believe the main piece of knowledge you are missing out on is the use of [OR] - which means a RewriteRule won't fail if only one of the RewriteCond results in true.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteRule ^(.*)/?(.*)$ https://example.com/?%1%2 [R=301,L]
You can test this out here: https://htaccess.madewithlove.be?share=f4381086-80b4-52b6-883e-8921f5de2316

After alot of help this solved my issue of stripping facebooks tracker, keeping my own query string and confirming https:// is used.
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteRule ^(.*)/?(.*)$ https://example.com/?%1%2 [R,N]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)/?(.*)$ https://example.com/$2 [R,L]

Related

how to redirect on single type of url

I want to redirect on single domain URL on like this https://www.example.com/
when user will type any one of belove URL
https://example.com/ or
www.example.com or
http://example.com
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^index$ "https\:\/\/www\.example\.com\/" [R=301,L]
this works for me
This is what you need.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com\/
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

How to add HTTPS via .htaccess file

I have installed an SSL on my website and I have a .htaccess file that has some conditions already written. I have looked at a number of methods shown online on how to force https. However, none of them seem to work. I keep having broken links.
The code I am currently using, the rule commented out does not work for https
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#This condition doesn't seem to work
#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect non www. to www.
#RewriteCond %{HTTP_HOST} !^www\.MYWEBSITE\.co\.uk
#RewriteRule (.*) http://www.MYWEBSITE.co.uk/$1 [R=301,L]
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1? [R=301,L]
# Remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php)
RewriteRule ^(.+)\.php /$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
If you would like to force HTTPS you can use the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Attempting to remove "www" and force "https" on domain and trying to make it do both

RewriteEngine On
#Remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
#force https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#ignore folder for removing slash & removing .html
RewriteRule ^(whmcs) - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
So this is my current code. Here is my results:
website.com --> https://website.com
http://website.com --> https://website.com
www.website.com --> https://website.com
http://www.website.com --> https://website.com
All good except...
https://www.website.com --> https://www.website.com <--No good.
I have been researching and attempting to rewrite this to no success. Please any help and if you would be so kind to leave documentation so I could read further. Either way I need a solution. Thank you!
You have written so many re-writing conditions to remove www and force https. If they not any other specific needs then you can just try this simple solution
RewriteRule (.*) https://website.com/$1 [R=301,L]
I had the same issue before, here is how i fixed it.
### WWW & HTTPS
#Remove WWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### WWW & HTTPS

Infinite loop htaccess

Im trying to make a simple html web page in 3 languages. Each language has its directory, so the index.html in "/" is in english, the spanish index in "/es" and the french index is in "/fr". Im trying to create a redirection rule for each language based on browser language or a cookie establised in the href onclick by javascript. This is my .htaccess code:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !^.*lang=ES.*$ [OR]
RewriteCond %{HTTP:Accept_Language} (es) [NC]
RewriteRule .* http://myweb.com/es/$1 [R=301,L]
RewriteCond %{HTTP_COOKIE} !^.*lang=FR.*$ [OR]
RewriteCond %{HTTP:Accept_Language} (fr) [NC]
RewriteRule .* http://myweb.com/fr/$1 [R=301,L]
RewriteRule .* http://myweb.com/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
It is not working because a infinite loop.
Thank you very much.
Redirect loop is happening due to unconditional last rule RewriteRule .* http://myweb.com/$1 [R=301,L]. Your first 2 rules also have have an error where they are not capturing any group but using them as $1.
Have your rules this way:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !^.*lang=ES.*$ [OR]
RewriteCond %{HTTP:Accept_Language} (es) [NC]
RewriteRule ^((?!es/).*)$ http://myweb.com/es/$1 [R=301,L,NE]
RewriteCond %{HTTP_COOKIE} !^.*lang=FR.*$ [OR]
RewriteCond %{HTTP:Accept_Language} (fr) [NC]
RewriteRule ^((?!fr/).*)$ http://myweb.com/fr/$1 [R=301,L,NE]
RewriteRule ^((?!(?:es|fr)/).*)$ http://myweb.com/$1 [R=301,L,NE,NC]
What happens is that each time a user enters a page they get redirected.
e.g.: someone has a cookie that says "es" but they go to the es directory, you will still redirect them to the es page, over and over again.
You should ad a condition saying on what page each redirect should work.
something like RewriteCond %{REQUEST_URI}
edit:
Here goes:
RewriteCond %{HTTP_COOKIE} !^.*lang=ES.*$ [OR]
RewriteCond %{HTTP:Accept_Language} (es) [AND]
RewriteCond %{REQUEST_URI} !^myweb.com/fr.*$
RewriteRule .* myweb.com/es/$1

Exclude certain subfolders and domains in redirects

This is a continuation from Redirect only HTML files?
How can I change my .htaccess to make it exclude certain subfolders or subdomains from the HTML-only redirect? I tried doing using this code to exclude the 'downloads' subfolder and the 'dev' and 'support' subdomains, but it didn't work:
RewriteCond %{HTTP_HOST} ^pandamonia.us$ [OR]
RewriteCond %{HTTP_HOST} ^www.pandamonia.us$ [OR]
RewriteCond %{HTTP_HOST} !download [OR]
RewriteCond %{HTTP_HOST} !faq
RewriteCond %{HTTP_HOST} !support [OR]
RewriteRule /.+\.html$ "http\:\/\/pandamonia\.us\/" [L]
You need to check REQUEST_URI or the whole match of the RewriteRule $0 for this; HTTP_HOST does only contain the host name of the current request. You also need to change the logical expression of your condition:
RewriteCond %{HTTP_HOST} ^pandamonia\.us$ [OR]
RewriteCond %{HTTP_HOST} ^www.pandamonia\.us$
RewriteCond %{REQUEST_URI} !^/download/
RewriteCond %{REQUEST_URI} !^/faq/
RewriteCond %{REQUEST_URI} !^/support/
RewriteRule /.+\.html$ http://pandamonia.us/ [L]
For those looking for a quick bit of insight into Gumbo's previous reply (where he mentions the situations for when to (and not to) use [OR], I found this WMW thread very helpful: http://www.webmasterworld.com/apache/3522649.htm