Handle languages in .htaccess - apache

I want to solve the next problem:
1) When entering the url without language, use English language as default
example:
http//localhost/plants/ or http//localhost/plants
or
http//localhost/plants/shop/accessories
2) When entering the url with language, pass that parameter as the language to use
example:
http//localhost/plants/es/shop/accessories
or
http//localhost/plants/es/ or http//localhost/plants/es
So far I have tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|es)/(.*)$ index.php?url=$2&lang=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?url=$1&lang=en [L,QSA]
If I comment the first RewriteRule and enter any url it works fine but always using the en language.
If I comment the second RewriteRule and I use a url with the language:
http//localhost/plants/es/
or
http//localhost/plants/es/shop/accessories
It works fine, but it doesn't set the default language to English when is not given.
Any idea why it doesn't work when I leave the two rules?
Thanks
Ps: I have removed the : after http

If plants is your document root, you should be able to use this rule.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !/(en|es)
RewriteRule ^(.*)/?$ index.php?url=$1&lang=en [L,QSA]
RewriteRule ^(en|es)/?(.*)/?$ index.php?url=$2&lang=$1 [L,QSA]
Let me know how it works for you.

Found a solution doing this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|es)/(.*)$ index.php?url=$2&lang=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1&lang=en [L,QSA]
How can I improve this? It feels wrong I have to write twto time the same Cond

Put the following htaccess file inside the /plants directory:
RewriteEngine On
RewriteBase /plants
RewriteCond %{REQUEST_URI} !^/plants/e[ns](/|$) [NC]
RewriteCond %{QUERY_STRING} !lang=e[ns] [NC]
RewriteRule ^.*$ /plants/en/$0 [R=301,L]
RewriteRule ^(e[ns])/(.*)$ index.php?url=$2&lang=$1 [NC,L,QSA]
If the above still doesn't work, update your own approach to something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^(en|es)/(.*)$ index.php?url=$2&lang=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?url=$1&lang=en [L,QSA]

Related

.HTACCESS Merging Two Rules

1.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ Ena.php?open=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^([^=]+)$
RewriteRule ^/?$ Ena.php?open=%1 [L,QSA]
2.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule "^(\x41|\x4F|\x53|\x4C\x4A|\x4E\x4A|D\xC5\xBD|\xC5\xA0|\xC4\‌​x90|\xC4\x8C|\xC4\x8‌​6|\xC5\xBD|\x00-\xF4‌​8FBFBF|[A-Z])[^/]*$" "/? open=Encyclopedia&letter=$1&term=$0" [NE,L,QSA]
I need these two Rules properly merged together. They work well alone (when one is deleted, and the other one left), but together they won't.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L,END]
RewriteRule "^(\x41|\x4F|\x53|\x4[CE]\x4A|D\xC5\xBD|\xC5\xA0|\xC4(?:\‌​x90|\x8C|\x8‌​6)?|\xC5\xBD|\x00-\xF4‌​8FBFBF|[A-Z])[^/]*$" "/? open=Encyclopedia&letter=$1&term=$0" [NE,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ Ena.php?open=$1 [L,QSA]
RewriteCond %{QUERY_STRING} ^([^=]+)$
RewriteRule ^/?$ Ena.php?open=%1 [L,QSA]
What happens if you put them in the order above? First, we check if requested content exists on system, and serve it as is. The END directive is new to httpd-2.4+, and short-circuits all other directives.

.htaccess rewrite to add country code (if missing)

I am using a .htaccess file in the subdirectory /cms and using this subdirectory as RewriteBase. The redirections go to 'backend.php' and send the variables I want. It is a dual language site (nl|en), dutch and english. Everything works fine, as long as the %{REQUEST_URI} starts with (nl|en).
But I need a fallback to the default dutch language when nl|en is omitted, but can I add this to the following .htacces file. I have been trying and searching but cannot find the right syntax to make this happen:
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_URI} !(^(nl|en)/)
#RewriteRule (.*) This is where the solution should be used ?
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule ^(nl|en)/([^/]+)/$ backend.php?page=$2&language=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule (nl|en)(.*)/(\d+)/$ backend.php?page=$2&id=$3&language=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule (nl|en)(.*)/(\d+)/(.+)/$ backend.php?page=$2&id=$3&task=$4&language=$1 [QSA,NC,L]
One way to handle this is a rewrite with the dutch language prepended. This will silently change direction to the proper page.
RewriteCond %{REQUEST_URI} !^/cms/(?:nl|en)/
RewriteRule ^(.*)$ nl/$1 [L]
If you want the client to notice and change the URL, you must do a R|redirect instead
RewriteCond %{REQUEST_URI} !^/cms/(?:nl|en)/
RewriteRule ^(.*)$ nl/$1 [R,L]
Your default rule can be this one:
# This is where the solution should be used ?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(nl|en)/ [NC]
RewriteRule ^([^/]+)/?$ backend.php?page=$1 [QSA,L]

Dynamic htaccess code for multiple php parameters to friendly urls

I have searched and searched for an answer to this, but none of the posts I've found on stackoverflow work for me - nor do the online htaccess generators.
I have made slight adjustments to Anubhava's excellent answer on htaccess redirect for dynamic urls not working to suit different page names as follows:
RewriteEngine On
# for external redirection from `/hp.php?su=sitename` to `/sitename`
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# for internal redirection from `/sitename` to `/hp.php?su=sitename`
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
However, in addition to sending users to domain.com/sitename (which the modified code above does) I also want to change the following urls.
domain.com/newpage.php?su=sitename&PgID=1234&pu=pagename
to become
domain.com/sitename/1234/pagename.html
similary
domain.com/diary.php?su=sitename
to become
domain.com/sitename/diary.html
This last one would be replicated for similar dynamic pages, such as
future.php?su=sitename >> domain.com/sitename/future.html
photos.php?su=sitename >> domain.com/sitename/photos.html
etc
Some time in the future, I would also like to divert http:// to https:// - would this rule go before all the others?
Hope somebody can help
UPDATE:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/newpage\.php\?su=([^\s&]+)&PgID=(\d+)&pu=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(diary|future|photos)\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%2/%1.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(diary|future|photos).html$ /$2.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/(.+)\.html$ /newpage.php?su=$1&PgID=$2&pu=$3 [L,QSA]
It isn't all that difficult to derive from the previous rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/newpage\.php\?su=([^\s&]+)&PgID=(\d+)&pu=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(diary|future|photos)\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%2/%1.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(diary|future|photos).html$ /$2.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/(.+)\.html$ /newpage.php?su=$1&pgID=$2&pu=$3 [L,QSA]
As to your second question about http to https, yes; that particular redirection should occur before all others.

I want to be able to search in two sub-directories with .htaccess

I want to be able to search in two sub-directories with .htaccess
I have the following directory structure
My problem is:
When for example a request on
resources/images/players_small_pigs/BMI10.png
is being received, I would like to be able to search in the path
webapp/public/resources/images/players_small_pigs/BMI10.png
is the file not to find here, I would like to continue the search in
webapp/adminpanel/resources/images/players_small_pigs/BMI10.png
Can some one help me to get it to work by using the .htaccess file? The problem throughout the entire folder structure resources, views, and webservices, I think it might be a bad structure I have and maybe I should put them all in a folder, rather than having it split up between a adminpanel and public folder, what would you recommend?
I have the following code in .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(resources/.*)$ /webapp/public/$1 [L]
RewriteRule ^(resources/.*)$ /webapp/adminpanel/$1 [L]
RewriteRule ^webservices/(.*)$ /webapp/public/webservices/$1 [L]
RewriteRule ^webservices/(.*)$ /webapp/adminpanel/webservices/$1 [L]
RewriteRule ^(public/resources/images/)(sponsors/)?(.*)$ /webapp/$1$2$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} adminpanel
RewriteRule (.*) /webapp/adminpanel/views/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !adminpanel
RewriteRule (.*) /webapp/public/views/$1 [L]
RewriteRule ^$ /webapp/public/views/index.php [L]
You can give the following rule a try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/resources
RewriteCond %{DOCUMENT_ROOT}/webapp/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/webapp/public%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/resources
RewriteCond %{DOCUMENT_ROOT}/webapp/adminpanel%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/webapp/adminpanel%{REQUEST_URI} [L]

RewriteRule to redirect whole domain to get parameter

I want to redirect a whole url to a query parameter with a RewriteRule in .htaccess
for example: http://server.com/http://google.com should be redirected to
http://server.com/index.php?url=http://google.com
so far i'm just able to make this work: http://server.com/google.com but when a : or / is contained, it doesn't work..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9_.-]+)$ index.php?url=$1 [L,NC,QSA]
thanks for help!
RewriteRule patter strips multiple / into one, better use RewriteCond here:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.php?url=%1 [L,NC,QSA]
Why not just do the TLD and then add the http:// in the rule. This is how I would do it.
This is the way I would use it so it doesn't "look" invalid. http://server.com/google.com
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9_.-]+)$ index.php?url=http://$1 [L,NC,QSA]