htaccess redirect (multidomain multilanguage) subfolder wildcard - .htaccess

I have a typo3 installation with multidomain and multilanguage, where not every language is setup to every domain.
Languages are de/fr/en/pt/es/cn/
www.example.de can be de/en/fr but not
www.example.de pt/es/cn
now I have messed up sth. and google has indexed loads of wrong urls e.g.
www.example.de/pt/
www.example.de/es/
www.example.de/cn/
they point to languages that are not set for this domain
I am fiddling around in the htaccess to redirect 301 the wrong urls with wildcards(?) to the .tld
I am looking for a solution redirect eg.
www.example.de/pt/* to www.example.de/
www.example.de/es/* to www.example.de/
www.example.de/cn/* to www.example.de/
where the * should represent the complete sting/path following the language parameter.
and of cause the same procedure for the .com domain
www.example.com/fr/* to www.example.com/
www.example.com/de/* to www.example.com/
I searched the web up and down but nothing I tried works.
any help would highly apreciated.
a tiny step further
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^cn/(.*)$ http://www.example.de/ [L,R=301]
RewriteRule ^pt/(.*)$ http://www.example.de/ [L,R=301]
RewriteRule ^es/(.*)$ http://www.example.de/ [L,R=301]
this seems to work
and now for the second domain as I need to differentiate between .com and .de
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^de/(.*)$ http://www.example.com/ [L,R=301]
RewriteRule ^fr/(.*)$ http://www.example.com/ [L,R=301]
this now breaks www.example.de/fr/whatever and redirects it to www.example.com as well.
so it looks like the first condition is matching and the the last rule is applied for french.
how can I limit the rules assigning them only to the appropriate domain conditions?

ok looks like every condition and respective rule needs to be written in a single line an repeated
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^cn/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^pt/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^es/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^de/(.*)$ http://www.example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^fr/(.*)$ http://www.example.com/ [L,R=301]
this seems to work.
any better solution is highly appreciated

Related

.htaccess language subdomain redirect

I am trying to redirect my language variables domain.com?lang=X to subdomains X.domain.com
I have this code:
RewriteCond %{QUERY_STRING} (^|&)lang=(es|de|fi|tr)
RewriteRule ^(.*)$ https://%2.domain.com%{REQUEST_URI} [R=301,L]
Unfortunately, when I want to change language by going to domain.com/page?lang=es, I'm redirected to es.domain.com/page?lang=es which causes a loop.
So I tried adding a questionmark to the end of the URL like so:
RewriteRule ^(.*)$ https://%2.domain.com%{REQUEST_URI}? [R=301,L]
to prevent it from adding the ?lang= variable when accessed by the subdomain but in that case the language doesn't change and I end up with es.domain.com/page in English.
I've spent the last 4 hours digging through StackOverflow and other sites trying to find a solution but to no avail. How can I detect that I'm already redirected to the subdomain and then ignore the ?lang= variable while actually changing the language?
Please help.
Thank you.
You will need 2 rules for this:
RewriteEngine On
# external redirect rule using THE_REQUEST variable
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+([^?]*)\?&?lang=(es|de|fi|tr)\s [NC]
RewriteRule ^ https://%2.domain.com/%1? [NE,R=301,L]
# internal rewrite rule to add lang parameter back
RewriteCond %{QUERY_STRING} !(^|&)lang= [NC]
RewriteCond %{HTTP_HOST} ^(es|de|fi|tr)\. [NC]
RewriteRule ^ %{REQUEST_URI}?lang=%1 [L,QSA]
Could you please try following .htaccess Rules at the top of your htaccess file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##use THE_REQUEST variable to match condition here, with NC flag.
RewriteCond %{THE_REQUEST} \s/([^?]*)\?&?lang=(es|de|fi|tr)\s [NC]
RewriteRule ^ https://%2.domain.com%1 [NE,R=301,L]

Redirect after a RedirectRule has taken place to other website

I have a website, let's say www.example.com but this used to be www.example.nl. All traffic from www.example.nl is now redirected to www.example.com, but as we changed some naming conventions, www.example.nl/seeds now has to redirect to www.example.com/nl/flower-seeds.
The .htaccess file I got contains the following code:
RewriteEngine On
RewriteBase
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
When I navigate to www.example.nl/seeds/ I end up at www.example.com/seeds/ which ends up in a 404 because I'm missing the /nl/flower- part.
I think Redirect doesn't work properly when the URL is already altered by a RewriteRule. How would you tackle this problem? Any help is appreciated!
You should exclude /seeds/ directory form general rules like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteCond %{REQUEST_URI} !/seeds/(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^seeds/(.*)$ http://www.example.com/nl/flower-seeds/$1 [L, R=301]
Note: clear browser cache then test
Also , don't use regex with redirect like what you did :
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
Here this (.*) has no meaning , you could use either RedirectMatch or RewriteRule

htaccess redirect subdomain to directory

I need help to write proper rewrite rules in my htaccess files.
I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.
I managed to do that successfully with this piece of code:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
My problem now is to write something more specific for pages, for example :
fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)
BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)
Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :
Redirect 301 /discover/foo /bar/otherfoo
Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo
How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.
Thanks !
EDIT
Please note that it's for a wordpress installation, and the htaccess starts with :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First the these rules:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
In order to capture bot www & non-www requests for subdomain.
Also this rule :
Redirect 301 /discover/foo /bar/foo
Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]
Note: clear browser cache then test.

two urls one multilingual site with Joomla and .htaccess

I have built a multilingual site in Joomla! 3.1.x Dutch and English, using the multilingual functions native to Joomla! 3.1.x. I have two domain names that I want to go to this site, one to the Dutch side, the other to the English side.
http://www.internationalerozekerk.nl
http://www.internationallgbtchurch.org
Number 1 should go to: index.php?lang=nl
Number 2 should go to: index.php?lang=en
In the .htaccess I have added this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.org [NC]
RewriteRule (.*) ^internationalerozekerk.nl/index.php?lang=en$1 [L,R=301]
This redirects the English URL to internationalerozekerk.nl/index.php?lang=en. However, the address bar still reads: internationalerozekerk.nl/index.php?lang=en and not internationallgbtchurch.org
I haven't found anything to make the two URLs stay in the address bar.
Any suggestions?
Thanx,
Thom
You can use these rules instead for internal redirects:
RewriteCond %{HTTP_HOST} ^(www\.)?internationallgbtchurch\.org$ [NC]
RewriteRule ^$ /index.php?lang=en [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?internationalerozekerk\.nl$ [NC]
RewriteRule ^$ /index.php?lang=nl [L,QSA]
Reference: Apache mod_rewrite Introduction
Try removing the hostname and the R flag and remove the $1.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.org [NC]
RewriteRule ^(.*)$ /index.php?lang=en [L,QSA]
For the other site:
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.nl [NC]
RewriteRule ^(.*)$ /index.php?lang=nl [L,QSA]
Adding the $1 at the end appends the request URI, which will completely mess up the query string. If you want the path to be sent as another param, then you need to be explicit about it (for example, "path"):
RewriteCond %{HTTP_HOST} ^internationallgbtchurch.org [NC]
RewriteRule ^(.*)$ /index.php?lang=en&path=$1 [L,QSA]

Using htaccess to redirect requests from non-www to www (but with a twist)

I'm currently using htaccess to force all requests to use www. So:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
to redirect example.com/page to www.example.com/page.
However, this htaccess file is being used by several international versions of example.com. What code do I want that can also do the following:
example.de/page -> www.example.de/page
and
example.co.uk/page -> www.example.co.uk/page
etc.
Potentially, there could be dozens of versions of example.com, so I'm looking to avoid having to remember to edit htaccess every time we add a new country.
You can use server variables in your substition, so this should be doable with
RewriteRule ^(.*)$ http://www.${HTTP_HOST}/$1 [L,R=301]
Right: think I've actually got it cracked. (Or at least it looks like it works):
RewriteCond %{HTTP_HOST} ^example\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example\.%1/$1 [R=301,L]
seems to do the trick.
Test if the current host name does not begin with www. and add it if missing:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Resources