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
Related
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]
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
How would i remove www from my url and force https and remove .html all at the same time?
RewriteEngine on
# force ssl
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
# Remove .html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Try adding the following to your .htacces instead of your code above.
RewriteEngine on
#Force HTTPS.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Start rewrite.
RewriteBase /
# Load page without extension.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# 301 While Rewrite.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
# Handle Trailing Slashes.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]
# Rewrite rule to add .html extension back internally.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+[^/])$ $1.html
This will result in http://www.example.com/test.html to be handled by the browser as https://example.com/test while still loading the test.html file.
Put the following code at .htaccess in main directory
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://yoursite.com/$1 [L,R]
#the code above will redirect the entire site into https without www unless the request come with https://www .
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ https://yoursite.com/$1 [L,R]
#The code above will catch any request with www if passed first code.
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.html[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
#the code above will remove any .HTML.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# you can use the two lines if file without extension not work and you want to map them to .html files and if you want to go the same file .html without extension keep it as it is but , If you want to change it to same name .php file , replace $.html in the last line to $.php .
Is there any way to have the first URL redirect to the second URL?
http://www.example.com/podcast/episode.html
http://www.example.com/podcast/episode
Is there any way to force the .html extension to redirect to the non-html version of the URL, so as not to show up as duplicate copies of the same page.
My htaccess code at the moment is:
RewriteEngine On
RewriteBase /
#removing trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#non www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#shtml
AddType text/html .html
AddHandler server-parsed .html
#html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://example.com/ [R=301,L]
Is this possible?
Not entirely sure if I understand the question, but if you want anything ending in .html to redirect to the same URL without the .html, this should do the trick:
RewriteRule ^/(.*)\.html$ /$1 [R=301,L]
This is how you can do the redirection from html to non-html only.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[^\ ]+)\.html\ [NC]
RewriteRule ^ %1 [L,R=301]
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