set multi language - mod-rewrite

I'm trying to make a mod_rewrite which can set the language
the rewrite:
www.domain.com/da/page.php => www.domain.com/page.php?set_lang=da
So far I have comed up with this:
RewriteCond %{HTTP_HOST} domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/(da|en)/(.*)$ [NC]
RewriteRule ^(.*)$ /%2?set_lang=%1 [L]
but I can't figure out how you can send get vars with it too?
like this:
www.domain.com/da/page.php?cat=black&cow=ugly => www.domain.com/page.php?set_lang=da&cat=black&cow=ugly

yay! found a solution, but I don't know if it could be done in a better way?
RewriteCond %{HTTP_HOST} domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/(da|en)/(.*)(\?%{QUERY_STRING})?$ [NC]
RewriteRule ^(.*)$ /%2?set_lang=%1&%{QUERY_STRING} [L]

Related

Having difficulty OHS rewrite rule for multiple domains

I'm having a bit of difficulty with rewriting on Oracle HTTP Server for multiple domains that point to same IP address and port
Following is working
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
However when I try https://sub-doamin-2/analytic it redirects to the https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL
Tried RewriteCond ${HTTP_HOST} method with no luck. It just redirect to / (root)
RewriteEngine On
RewriteCond ${HTTP_HOST} sub-doamin-1$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-1/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
RewriteCond ${HTTP_HOST} sub-doamin-2$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub-doamin-2/analytics
Can you please assists resolving this issue?
It should be %{HTTP_HOST} instead of ${HTTP_HOST}
So the rules should be:
RewriteCond %{HTTP_HOST} sub1.test.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub1.test.com/psp/UACMP/SELF_SERVICE/SA/c/NUI_FRAMEWORK.PT_LANDINGPAGE.GBL [R,L]
RewriteCond %{HTTP_HOST} sub2.test.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://sub2.test.com/analytics [L]
You can check the rules here: https://htaccess.madewithlove.be?share=6632e45c-a7bb-5099-ab0b-468ba1066277
for the urls https://sub1.test.com and https://sub2.test.com
If you write your original rules in that website you will get This test string is not supported: ${HTTP_HOST} so this can also help you next time.

Rewrite example.txt to another based on the domain

Im using a multi-install for my webpage with two different domains and i need for each domain a unique robots.txt
like
https://www.domain1.tdl/robots.txt should use the https://www.domain1.tdl/robots_domain1.txt
and
https://www.domain2.tdl/robots.txt should use the https://www.domain2.tdl/robots_domain2.txt
Check this rewrites in .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.tdl [NC]
RewriteCond %{REQUEST_URI} ^\/robots\.txt$
RewriteRule (.*) https://www.domain1.tdl/robots_domain1.txt [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.tdl [NC]
RewriteCond %{REQUEST_URI} ^\/robots\.txt$
RewriteRule (.*) https://www.domain2.tdl/robots_domain2.txt [L]

mod_rewrite with external redirect and internal rewrite

I'm trying to use mod_rewrite to redirect certain pages to use SSL. For that I have:
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/\..*$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC]
RewriteRule ^(.+)\.php$ https://www.example.com/$1 [R=301,L]
This works fine, and does exactly what I want it to do.
Later in my .htacess I have a:
RewriteRule ^members/(.+)/change-password$ members/.change-password.php?item=$1 [NC,QSA,L]
So if a URL appears as, for example:
http://www.example.com/members/foo-bar/change-password
Internally it would be processed as:
/members/.change-password.php?item=foo-bar
Again, this works fine and is doing what I want it too.
What I now need to do is include this in my original SSL redirect logic to ensure that any change password requests are redirected to the same URL but over https instead. I've tried:
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/\..*$
RewriteCond %{REQUEST_URI} !^/members/.+/change-password [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/members/.+/change-password [NC]
RewriteRule ^(.+)\.php$ https://www.example.com/$1 [R=301,L]
But this doesn't work - I just get the page delivered over http. Changing the .+ to .* appears to put me into a permanent redirect loop.
I'm guessing this is because of the internal rewrite but no matter what I try I can't seem to resolve it.
Can anyone please advise?
Thanks,
Adam M.
A further review of the mod_rewrite documentation led me to a bit I'd missed specific to its usage in .htaccess files. Basically the [L] flag doesn't actually indicate last as per the norm. Instead you need to use the [END] flag (http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l refers).
Of course that then led me to another issue - my hosting provider doesn't have an up-to-date installation of either Apache or mod_rewrite so the [END] flag triggered the ubiqitous HTTP 500 Internal Server Error.
So what to do? Well I went back to my original ruleset with the knowledge that [L] wasn't doing what I was expecting and spotted the error straight away - the %{REQUEST_URI} value had been updated by the internal rewrite:
RewriteRule ^members/(.+)/change-password$ members/.change-password.php?url-slug=$1 [NC,QSA,L]
Therefore changing my original redirection logic to exclude this resolved my issue:
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{REQUEST_URI} !^/login(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/contact-us(\.php)?$ [NC]
RewriteCond %{REQUEST_URI} !^/\..*$
RewriteCond %{REQUEST_URI} !^/members/.+/change-password$ [NC]
RewriteCond %{REQUEST_URI} !^/members/\.change-password(\.php)? [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_URI} ^/login(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/contact-us(\.php)?$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/members/.+/change-password$ [NC]
RewriteRule ^(.+)(\.php)?$ https://www.example.com/$1 [R=301,L]

Mod_rewrite wildcard subdomains. Simple question regarding not so simple rewrite rules

basically I'm trying to do this:
subdomain.domain.com -> domain.com/account?user=subdomain
So far so good. I have found this code to do it and works perfect:
RewriteCond %{HTTP_HOST} !^domain\.com [NC]
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com$
RewriteRule ^(.*)$ account.php?user=%1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
Now I'm trying to add one more simple rule to it (marked with *). Adding this new rule makes everything fall apart. Nothing works anymore
RewriteCond %{HTTP_HOST} !^domain\.com [NC]
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com$
* RewriteRule ^/somepage http://domain.com/anotherpage [E=SUBDOMAIN:%1,L]
RewriteRule ^(.*)$ account.php?user=%1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
Any idea what is going on and how to correct it ?
Thank you
Catalin
A RewriteRule is subject to the current set of RewriteCond conditions.
So you will have to do something like that:
RewriteCond
RewriteCond
RewriteRule
RewriteCond
RewriteCond
RewriteRule

how to set language

how can I set the language (en, da, de etc) with mod_rewrite?
all files are located in the same dir
url to set language is
?set_lang=da
rewrite:
www.domain.com/en/index.php => www.domain.com/index.php?set_lang=en
www.domain.com/en/another_page.php?cat=black => www.domain.com/another_page.php?set_lang=en&cat=black
www.domain.com/da/index.php => www.domain.com/index.php?set_lang=da
www.domain.com/da/another_page.php?cat=black => www.domain.com/another_page.php?set_lang=da&cat=black
EDIT:
my .htaccess looks like this
RewriteCond %{HTTP_HOST} ^domain\.net$ [NC]
RewriteRule ^(.*)$ http://www.domain.net/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.domain\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/_domain\.net/ [NC]
RewriteRule ^(.*)$ /_domain.net/$1 [L]
RewriteCond %{HTTP_HOST} ^(demo|mysql|secure)\.domain\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/_domain\.net/_(demo|mysql|secure)/ [NC]
RewriteRule ^(.*)$ /_domain.net/_%1/$1 [L]
RewriteCond %{HTTP_HOST} domain\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/_domain\.net/ [NC]
RewriteRule ^(.*)$ http://www.domain.net/$1 [L,R=301]
I found a solution
RewriteCond %{HTTP_HOST} domain\.net$ [NC]
RewriteCond %{REQUEST_URI} ^/(da|en)/(.*)(\?%{QUERY_STRING})?$ [NC]
RewriteRule ^(.*)$ /%2?%{QUERY_STRING}&set_lang=%1 [L]

Resources