.htaccess rewrite to add country code (if missing) - apache

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]

Related

Infinite redirect after htaccess

RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus/? [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
I changed it for
xyz.com/worksheet/rebus/?random=testing11
to be forwarded for
xyz.com/worksheet/rebus/
buts its endup in infinite redirect.
With your shown samples, please try following htaccess Rules file. Please make sure you keep your htaccess Rules file along with worksheet folder NOT inside worksheet folder.
Clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus/? [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?tableName=$1 [L,QSA]

Handle languages in .htaccess

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]

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]

Tricky 301Redirect Rule

I want link to be redirected in a manner such that url with not slash at the end should be redirected to the url with the slash at the end.
example 1:
http://example.com/quiz/funny-riddles-with-answers
should be redirect to
http://example.com/quiz/funny-riddles-with-answers/
example 2
http://example.com/quiz/math-riddles-with-answers
should be redirected to
I have no idea how to do it.
my current screen shot of .htacess is
RewriteEngine On
RewriteBase /quiz/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ mypage.php?param1=$1&param2=$2 [L,QSA]
Try these rules in /quiz/.htaccess:
RewriteEngine On
RewriteBase /quiz/
# add a trailing slash for non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?[^/])[?\s]
RewriteRule ^ %1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ mypage.php?param1=$1&param2=$2 [L,QSA]
You can easily rewrite to a trailing slash using:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
Place above your already existing rewrites.
You need to change your current rule's pattern to: ^([^/]+)/([^/]+)?/?$
then add this right under RewriteBase /quiz/:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /$1/$2/ [L,R=301]

Rewrite rule to handle url exception

I have an app that serves many sites and I am using Apache mod-rewrite to map the
url's like this
http://site1.net/controller
http://site2.net/controller2/another_view
http://site3.net/
http://special_case.net/
maps to:
index.php?url=http://site1.net/controller
index.php?url=http://site2.net/controller2/another_view
index.php?url=http://site3.net/
index.php?url=http://special_case.net/hub
My rewrite rules are:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Which handles all the cases except the special last case, where I need to force
the use of a controller called "hub" when handling a specific domain. I collaborate with others on this project which means I can't do anything about the routing once the index file is called.
Can someone fix my rules so that all the above cases resolve?
You current rules don't seem to add the hostname to the url get-parameter. So I added that to to the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(special_case\.net)$
RewriteRule ^(.*)$ index.php?url=http://%1/hub/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)$ index.php?url=http://%1/$1 [QSA,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^http://special_case.net index.php?url=http://special_case.net/hub [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]