.htaccess rewrite / accept language - html

following problem:
I have 2 html sites in subfolders, one site in English and one in German. I use .htaccess rewrite rule to redirect to the right site based on the language of the user:
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ http://example.com/ [L,R=301]
This is the code in the .htaccess in the subfolder of the English version.
The problem occurs when a German user wants to view the English version of the page, he always gets redirected to the German version.
Is there any way to redirect the user on the first visit, but when he clicks on the hyperlink English on the German page, he gets to see the English version, without being redirected again to the German site?
mod-rewrite is available but I need a condition to redirect only once (on first visit) and when the user clicks a specific hyperlink, he wont get redirected again?

Just an idea, if you have two subdirectores, e.g. de and en, you could rewrite instead of redirect based on Accept-Language header. But if the browser requests an explicit path including the language prefix, it won't be redirected.
http://www.example.com/page1.html will show /en/page1.html or /de/page1.html depending on Accept-Language
http://www.example.com/en/page1.html will always show /en/page1.html no matter what Accept-Language says
You could try these (untested!) rules
RewriteRule ^en/ - [L]
RewriteRule ^de/ - [L]
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^ /de%{REQUEST_URI} [L]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^ /en%{REQUEST_URI} [L]
Another approach could be to set a cookie, when the user decides on a language, and deliver pages according to this cookie.

Related

send id via url but hidden

I have an html link:
Link
I formatted the link destination with a htaccess file:
RewriteRule ^destination$ index.php?content=destination [L,NC,QSA]
The question is: How can I give a ID via URL to the destination?
the full link will be index.php?content=destination?ID=x
X will be a dynamic number
but I would like to show this in the url - it should be invisible.
But I don't know how I have to modify the rewriteRule to realize it.
Although not a pretty solution and it obviously comes with flaws, you could always use cookies.
set a cookie in PHP:
setcookie("page_id","987987");
and look for it in htaccess
RewriteCond %{HTTP_COOKIE} ^page_id=([0-9]*)$ [NC]
RewriteRule ^destination$ index.php?content=destination&id=%1 [L,NC,QSA]

htaccess redirection loop issue

I have a static website that has several html pages. In order to not hide .html extensions from url, we have following rules in htaccess for each url:
RewriteRule ^website-development$ website-development.html
This works fine in that if you open site.com/website-development, it actually opens the website-development.html file but in url it does not show the .html extension.
Now, we need to redirect(or hide) .html urls to their corresponding urls without .html e.g. if someone opens site.com/website-development.html in their browser, the url should be shown as site.com/website-development.
To do this, I added following rule:
RewriteRule ^(.*)\.html$ /$1 [L,R]
But doing this results in indefinite redirection and so the browser just keeps redirecting and never renders the actual page. Can you please suggest how I can redirect both site.com/website-development and site.com/website-development.html to site.com/website-development (which actually is an html file - website-development.html ). Thanks.
Yes indeed, this rule will cause a redirect loop:
RewriteRule ^(.*)\.html$ /$1 [L,R]
It is due to the fact your REQUEST_URI is adding and removing .html in 2 different rules and mod_rewrite runs in a loop.
You can use this rule instead based on a RewriteCond that uses THE_REQUEST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1

HTML Place in folder while not placing it in the site directory? [duplicate]

My aim is:
domain.com/folder
rewrite ->
domain.com
this shall concern ALL links inside that site.
I mean on the site are links like:
domain.com/folder/forum.html
domain.com/folder/community.html
etc.
This is my aim:
domain.com/forum.html
domain.com/community.html
etc.
and its very important that the "folder" is never in the url in the adressbar visible.
I tried already many codes but I couldnt really solve this problem.
My best try was with this code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)folder
RewriteRule ^(.*)$ folder/$1 [L]
If I enter
domain.com
I get the content of
domain.com/folder
displayed, what is correct ("folder" is not in the url shown). But when i click on some links of the site like: domain.com/folder/community.html then I can see again "folder" in the url, but I want that it becomes ALWAYS removed.
here is my site:
thewedgiecommunity.x10.mx/wedgiecommunity/
My aim is to remove the "wedgiecommunity" (=folder)
This link is working
thewedgiecommunity.x10.mx/
But when you click on Community (
thewedgiecommunity.x10.mx/wedgiecommunity/community.html
) then i get again "wedgiecommunity" in the URL.
Would be awesome when someone could help me
You can use this code:
Goes in DOCUMENT_ROOT/wedgiecommunity/.htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+wedgiecommunity([^\s]*) [NC]
RewriteRule ^ %1 [R=301,L]
Goes in DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteRule !^/?wedgiecommunity wedgiecommunity%{REQUEST_URI} [L,NC]
You can use this rule to "remove" the folder from the URL when it is accessed directly via the browser:
RewriteCond %{THE_REQUEST} \ /wedgiecommunity/
RewriteRule ^wedgiecommunity/(.*)$ /$1 [L,R=301]
Then your other rule will handle the rest.

How can I redirect main domain to another domain but not include sub domains in the redirect - htaccess

I have the .co.uk and .com for for my domain and I currently have the .co.uk set-up to redirect to the .com. Whilst this works, I now need to refine this redirect a little so that it does not include any subdomains. So www.domainname.co.uk will redirect to another but subdomain.domainname.co.uk will not redirect.
Is this possible and if so how do I do it?
You may want to take a look at RewriteCond, something like this:
RewriteCond %{HTTP_HOST} www.\mysite\.co\.uk [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301]
For more in-detail information about RewriteCond: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond

Redirect only HTML files?

Part One
I want to .htaccess redirect all HTML files to the home page. I looked at this guy's question (htaccess redirect all html files), and wrote this code:
RewriteCond %{HTTP_HOST} ^pandamonia.us$ [OR]
RewriteCond %{HTTP_HOST} ^www.pandamonia.us$
RewriteRule .*\.html$ "http\:\/\/pandamonia\.us\/" [L]
but the problem is that it also redirects the homepage to itself, causing the universe to end.
So my question, is how can I redirect every HTML page that is not the homepage to the homepage.
 
Part Two
Exclude certain subfolders and domains in redirects
Try changing .* to .+ in the regexp, that should mean 'at least one character' instead of zero or more characters, so the empty string should be avoided.
Wait. The initial '/' is included. Try it like:
RewriteRule /.+\.html$ "http\:\/\/pandamonia\.us\/" [L]