How to apply a htaccess only when arriving on the website? - .htaccess

Let me explain the problem. We have a website in 2 languages: fr & nl (dutch).
When you arrive on our website, you land on www.domain.be which redirects
you (as you can see in the following code) to the dutch version if your browser language
is set to 'nl' and if it's set to 'en' (because dutch people often use this language for they browser) or leave you on www.domain.be / fr.domain.be (both url are work to call the website - the fr... one is more in response of the nl... one)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
</IfModule>
On the website, you can choose, by clicking on 2 links (in the top right corner), if you want to go to fr.domain.be or nl.domain.be.
When you click on one of those link, the htaccess redirects you even if you want to go to the fr part (while navigating on the dutch one) and the same on the nl part.
How can I solve that? I would like the htaccess to only redirect you when you first come to the website but then be desactivated and allow the user to choose his language if he wants to.
Could you please help me? I'm on this for like two days...

You're only redirecting the site base /, so swith directly to another page should not be a problem.
Can't you just use a fake index page when you manually switch ? Like http://nl.domain.be/index

You can create multiple conditions for a rule set so you could add an aditional check that the redirect only happens when you do not match the HTTP_REFERER to your domains.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
</IfModule>
the ! means NOT so basically you are saying when the HTTP_REFERER does NOT match the url pattern, which is "(wildcard).domain.be".
This will prevent the rule from being run if they are on your site currently and trying to change the language.
NOTE: I'm not near an apache box to test this so my syntax might be off but that should get you down the right path.

Related

Forced SSL in htaccess going to only the home page

I have recently added an SSL to my sites. I have added the code to the .htaccess file to force the https. The issue is that my external links that go to pages within the site are now being redirected to the homepage. The code I am using is:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.watsonelec.com%1 [R,L]
I think the issue is in the last line, as the rule is telling it to redirect to the homepage. What I can't seem to find is a rule that will say for it to go to the URL provided in the link but give it an https instead of the HTTP.
I did do a search for this topic, but all the code I found was similar to what I already had. Thank you for all your help.
Update
I have two sites I am trying to work this out for, watsonenerysolutions.com and watsonelec.com.
When I tried
RewriteOptions InheritDownBefore
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.watsonenergysolutions.com/$1 [R,L]
It still sent to the homepage
When I tried
RewriteOptions InheritDownBefore
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^ https://www.watsonenergysolution.com%{REQUEST_URI} [R,L]
I received an error message that said Safari can't open the page "https://www.watsonenergysolutions.com/index.php" because Safari can't find server "www.watsonenergysolutions.com"
%N backreferences are what you match in RewriteCond's. In your case, it is empty. That's why anything is going to the homepage.
You need to use $1 or %{REQUEST_URI}, both rules below are equivalent (the second may be faster because you don't -re-match unnecessarily)
RewriteRule ^(.*)$ https://www.watsonelec.com/$1 [R,L]
RewriteRule ^ https://www.watsonelec.com%{REQUEST_URI} [R,L]
Note 1: %{REQUEST_URI} value always begins with a leading /, while what you can match in a RewriteRule never begins with a leading /
Note 2: R flag uses a 302 redirect by default. Maybe you'll want to use a 301 ([R=301,L])

redirect users by language - too many redirects

I am trying to redirect users with Chineses languages to the a domain using the following code in an .htaccess file
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (zh) [NC]
RewriteRule ^(.*)$ http://www.example.com/under_c.html [L]
When I change my browser language to Chinese and test out the redirect it does go to the specified page, but it doesn't display anything it just gives me an error in the console that says "Failed to load resource: net::ERR_TOO_MANY_REDIRECTS". I've tried other solutions around the web but none of them seem to be able to redirect by language.
Is there a better way to redirect by language in the .htaccess file?
To prevent a rewrite loop you need to exclude under_c.html from the rule.
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (zh) [NC]
RewriteCond %{REQUEST_URI} !=/under_c.html [NC]
RewriteRule ^ http://www.example.com/under_c.html [R=302,L]

Rewrite subdomain url to root domain url

I use the following rules to rewrite a subdomain call to a page in the root of the website:
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9/_.-]+)\.domain\.com$ [NC]
RewriteRule .? headlines.php?url=%1 [L,QSA]
This works fine. I use this for a rss - news related website. For example: http://economy.headlines.com will internally look at http://www.headlines.com/headlines.php?url=economy
I also want to link to the news items in the following way:
economy.headlines.com/news/title/id
How do i do this ? Because every time the first rules are "fired". Even if i make other rules with the [L] flag the other rules are fired and nothing happened.
How can i combine the rules above with new rules which also look at files in the root of the site but with parameters in the url ?
You should be able to combine both if you evaluate the path component of the request you rewrite:
Rewriteengine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9/_.-]+).domain.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ headlines.php?url=%1&category=$1&title=$2&id=$3 [L,QSA]
Probably you have to do some fine tuning, but I think you get the idea: the first argument of the RewriteRule is a regular expression that splits the request path into its components. Those can be referred to using the $1 and $2 notation you can see towards the end of the rule.
Edit: added the category parameter to the RewriteRule as discussed in the comments below.

RewriteRule [L] still changing the url

I found myself with this problem, which is driving me a little bit crazy. I use apache's mod_rewrite for pretty URLs and I need to use dynamic subdomains in the site. Everything is great and all the server has de wildcards. I use the next code on my .htacess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/%1 [L]
The only problem is, even if I use the [L] flag the url of the site change to http://mysite.com/subdomain. What i want is the url to be like http://subdomain.mysite.com
The link mysite.com/subdomain is a dynamic url and is solved with another rule with the following code:
RewriteRule ^([A-Za-z]+)$ filter.php?type=subdomain&subdomain=$1
Any help would be appreciated
If you specify an external URL (which changing the subdomain does), a header redirect will take place. I don't think you can prevent that. But why not skip that step altogether, and use the second RewriteRule straight away?
I can't test this right now, but something like
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ filter.php?type=subdomain&subdomain=$1
should work.

.htaccess which somehow prevents Google to put some PageRank on my domains

I am using the following .htaccess code on all my domains since 2+ years ago on some projects, but no one of the websites build has ever got any Google PageRank, at least '1' bar. On all websites on which I don't use this code, I am getting a reasonable PageRank.
Could you tell me what I am doing wrong:
RewriteEngine On
RewriteBase /
# rewrite the non 'www' addresses
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# rewrite REQUEST_URI
RewriteCond %{HTTP_HOST} ^www\.example\.com [OR]
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) index.php [L]
some of my websites using this .htaccess:
http://www.kampril.bg/
http://www.milleniumbg.eu/
Register these domains in the Google Search Console and check whether Google returns some error messages or some feedback about these. Submit some sitemaps.
If you do not see any error messages or warnings, then it simply means Google does not find the content of your websites interesting.

Resources