Language Rewrite .htaccess - .htaccess

Sigh...
I tried constructing some rewrite rules based on the following Stack Overflow questions:
Question 1
Question 2
My .htaccess currently looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(en|fr|es|de)/
RewriteRule ^(.*)$ /en/$1 [R,L]
RewriteRule ^(en|fr|es|de)/(.*)$ $2?locale=$1 [L]
and I've tried all sorts of variations (including the exact answers given in the questions).
As it stands my URL is redirecting to:
/en/?locale=en
When I look at the root with what looks like an infinite redirect loop in Firefox ('The page isn't redirecting properly').
As you've probably guessed I want it to rewrite to /en/ on the root and for it to look like that in the browser. But I want the real url to be /?locale=en. There is an index.php there on the root. Maybe I need another rule to take that into account?
I don't know, I'm really tired and exasperated and .htaccess has always been my downfall. Any insight appreciated.

You need to make sure there's no locale query string in your first rule:
RewriteCond %{QUERY_STRING} !locale=
RewriteCond %{REQUEST_URI} !^/(en|fr|es|de)/
RewriteRule ^(.*)$ /en/$1 [R,L]
Or you can try:
RewriteCond %{THE_REQUEST} \ /+(?!(en|fr|es|de)/).*
RewriteRule ^(.*)$ /en/$1 [R,L]

Related

Root redirects to just one subdomain in .htaccess

I want the root of my website (www.bitumephotofest.it) to redirect to a subdomain (2016.bitumephotofest.it) (I am running a Wordpress multisite). It works.
But I have another subdomain (2015.bitumephotofest.it) and it also redirects to 2016.bitumephotofest.it.
I want the redirect to work only between www.bitumephotofest.it and 2016.bitumephotofest.it. 2015.bitumephotofest.it should be independent as it is a different website.
I tried to look for questions by people with a similar situation but there is always something different and, anyway, those solutions does not work for me.
Here is my code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 2016.bitumephotofest.it
RewriteCond %{REQUEST_URI} !wordpress/
RewriteRule ^(.*)$ /wordpress/$1 [L]
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteRule !^(2016) http://2016.bitumephotofest.it [L,R]
Does anyone know what I am missing?
Thank you in advance!
If you want the domain 2015.bitumephotofest.it stay unmodified, you can sort of exit the rewrite rule chain with
RewriteCond %{HTTP_HOST} ^2015\.bitumephotofest\.it$
RewriteRule ^ - [L]
- as the target means don't rewrite, see RewriteRule
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.

htaccess: Rewrite if url is www.domain.com/some-string/xxx but not www.domain.com/some-string-is-here

My htaccess looks like the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^some-string/$ [NC]
RewriteRule ^some-string(.*)$ http://www.domain.com$1 [R=301,L]
So all requests from www.domain.com/some-string/xxx should be redirected to www.domain.com/xxx (from subfolder to main domain). Now I had the problem that an article contained exactly the some-string part in his name e.g. www.domain.com/some-string-is-here. The result is that the article is not found.
How do I have to adapt the redirect?
Making the RewriteRule a bit more specific should help; in that case the RewriteCond is not necessary. As only requests starting with some-string/ (including the /) should be rewritten, appending that / to the rule does the trick for me:
RewriteEngine On
RewriteBase /
RewriteRule ^some-string/(.*)$ http://www.domain.com/$1 [R=301,L]
If you also want to rewrite www.domain.com/some-string (and only this and not www.domain.com/some-string-more), you can try instead:
RewriteRule ^some-string(/.*)?$ http://www.domain.com$1 [R=301,L]

Specific .htaccess redirect based on query_string

I need this logic to work:
I want rewrite this string for users to see
http://mysite.com/index.php?cl=mykeystring
to
http://mysite.com/otherkey/
http://mysite.com/index.php?myvar=test&cl=mykeystring&mysecondvar=morevalue
to
http://mysite.com/otherkey/myvar=test&mysecondvar=morevalue
But when http://mysite.com/otherkey/ is written, so load
http://mysite.com/index.php?cl=mykeystring, but no redirects will be done.
Is it possible? There are no possibility to change anything in codes, but only .htaccess
This logic is nearly realized by this code:
RewriteCond %{QUERY_STRING} ^(.*?)cl=mykeystring(.*?)$ [NC]
RewriteRule ^index.php$ /otherkey/%1%2? [R,L]
RewriteRule ^otherkey/(.*?)$ /index.php?cl=mykeystring&$1
but im getting some not needed amp symbols on first rewrite rule. any solutions?
I think you can do something like this:
RewriteCond %{QUERY_STRING} cl=mykeystring [NC]
RewriteRule ^index.php /otherkey/ [QSA]
Docs here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

How to forward to new URL including all the variables?

I have been providing my subdomain name to friend until he manages to get new domain.
Now he has it and I would like to 301 redirect all the old links to his domain:
Example:
http://subdomain.my-domain.com/post.php?118&tg=602643
Redirect to
http://subdomain.com/post.php?118&tg=602643
So I want to keep up all the variables behind the post.php in the redirect
I am .htaccess newbie - can you please help me with providing the correct Rewrite rule?
Also, If you happen to have any good article about .htaccess and how to manage it, link is really appreciated.
Thanks
EDIT
Here are actual usecases I want to do:
redirect
http://raketa2.tasselhof.com/nastenka.php?115&up=648483
to
http://www.raketa2.cz/nastenka.php?115&up=648483
However, since there is no more subdomain existent on my site, i see the 404 error on my main page as this:
/nastenka.php?115&up=648483 -> Provided 404 Error
I tried:
RewriteCond %{Request_URI} ^/nastenka\.php [NC]
RewriteCond %{QUERY_STRING} ^\d+&tg=\d+$ [NC]
RewriteRule ^(.*)$ http://www.raketa2.cz/$1?%{QUERY_STRING} [R=301,L]
But with no good...
SOLVED
Duh! I am really dumb. I just added two new CNAME records to my domain DNS. Should do the trick
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]
The QUERY_STRING post.php?118&tg=602643 will be present.
Assuming that there is no post.php on your domain,
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{Request_URI} ^/post\.php [NC]
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]
If post.php is present on your domain but, you do not use similar query strings,
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{Request_URI} ^/post\.php [NC]
RFewriteCond %{QUERY_STRING} ^\d+&tg=\d+$ [NC]
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]

.htaccess and url rewriting challenge

My problem is very simple.
I have several folders in my root:
[folder1]
[folder2]
[folder3]
And my domain is something like: http://johndoe.net
I keep my website in [folder1], so basically I access it by typing http://johndoe.net/folder1
My goal is this:
I want to type http://johndoe.net and I get the content of http://johndoe.net/folder1 but the address has to remain http://johndoe.net
I want to type http://johndoe.net/folder1 and see my website, but my address has to change to http://johndoe.net
Seems easy enough, but I failed finding the solution after several hours of searching.
So far, the only thing I achieved is redirecting from http://johndoe.net to http://johndoe.net/folder1 by placing this bit of code in my .htaccess in my root:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^johndoe\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.johndoe\.net$
RewriteRule ^/?$ "http\:\/\/johndoe\.net\/folder1\/" [R=301,L]
When I type http://johndoe.net, I get http://johndoe.net/folder1 in my address bar
but what I need is my address to remain http://johndoe.net
Could anyone help me with this?
You're going to use two .htaccess files to solve this problem. Here's what you put in them, and where they go.
File #1. This goes in your base folder (the one that contains [folder1],[folder2], etc)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((www\.)?johndoe.net)$
RewriteRule ^(.*)$ /folder1/$1 [L]
File #2. This goes in [folder1]
RewriteEngine Off
Try something like this
RewriteRule ^(.*)$ /folder1/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^johndoe\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.johndoe\.net$
RewriteRule ^/?$ "\/folder1\/" [L]
you need internal rewrite, So it's enough to rewrite / to /folder1/

Resources