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]
Related
How can I add the Google translate parameter #googtrans(en|de) or other language, so the translation happens automatically?
Basically, when the user goes to https://example.com/page/?lang=de they are redirected to https://example.com/page/?lang=en#googtrans(en|de)
I use this .htaccess rule, but it's not working:
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^/?lang=en#googtrans(en|[a-z]{2}) [R=301,L]
EDIT: Adding edited Rules here.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/page/?\?lang=([a-z]{2})\s [NC]
RewriteRule ^ page/?lang=%1#googtrans(%1) [R=301,L,NE]
With your shown samples(this is considering that you are hitting URL like: https://example.com/page/?lang=de in browser), please try following .htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$ [NC]
RewriteRule ^page/?$ page/?lang=%1#googtrans(%1) [R=301,L,NE]
I am working on a php script on which i will generate links with random subdomains.For example : x.domain.com sius.domain.com x5-.domain.com and so on. In fact these subdomains doesn't really exist what i want is that when user goes to any link (RANDOM).domain.com it shows the content of domain.com/result.php?rand=(RANDOM).
PS:I considered (RANDOM) as the variable. and i want to exclude www.
I tried this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ http://domain.com/result.php [L,R]
But nothing seems to work. Can anyone help ?
Try with:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteRule ^ /result.php?rand=%1 [L,QSA]
You can't redirect (http://... and [R]) without link change. With this code you rewrite without redirection.
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
Here is what I need to redirect to a temporary HTML page:
http://www.domain1.com/?Itemid=230
should get redirected to:
http://www.domain2.com/temoporary-solution.html
Here is what I came up with, just not sure if it will cause any issues between the rest of the .htaccess rules (this is the first rule):
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [NC]
RewriteCond %{QUERY_STRING} ^Itemid=230$ [NC]
RewriteRule ^$ http://domain2.com/temoporary-solution.html [R=302,NE,NC,L]
Your rule should work fine. Just append ? at the end of target URI to strip off existing query string:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{QUERY_STRING} ^Itemid=230$ [NC]
RewriteRule ^$ http://domain2.com/temoporary-solution.html? [R=302,L]
Those rules are fine. The conditions are pretty strict so as long as it's the first rule, it won't break anything else.
I have this link: http://www.domain.com.mk/lajmi.php?id=2790,
and i want to change it to http://www.domain.com.mk/lajmi/2790
With this code I can change the link to /lajmi/2790 but i get 404 error.
I mean i get the link
http://www.domain.com.mk/lajmi/2790, but it has 404 error (i dont se the content)
This is my code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
What I am doing wrong ?
Try this one :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1&r=0 [L]
(the &r=0 in the final rule is for not getting an infinite loop)
Single direction rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L,QSA]
This means that every uri of kind /lajmi/2790 will be passed to /lajmi.php?id=2790 in a sub-request.
However, in this case, if the user hits /lajmi.php?id=2790 by himself, then this is the url he will see in the browser, not the "beautified one".
Bi-directional rewrite:
RewriteEngine On
RewriteBase /
; Redirect lajmi.php?id=2790 to a beutified version, but only if not in sub-request!
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{IS_SUBREQ} !=true
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ lajmi/%1 [R=301,L]
; Make the beutified uri be actually served by lajmi.php
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L]
Here, an additional RewriteCond was added to the first rule checking that this is not a sub-request, to ensure that the rules do not loop.
You can pick which way you like, but the first approach is enough if you build the links in your HTML in the 'beautified' way already (no need to redirect the browser twice just to see the page).