Check REQUEST_URI for any /nl/ or /en/ to change my RewriteRules

19 views Asked by At

I have my own CMS system with settings for an multi language website and I save this in my database. But I can't check that variable in my htacces so I want to check my request url like this:

cms2024.nl/nl/home or cms2024.nl/home and then give different RewriteRules to get my right url order

If there is an language in the url I want these rules:

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /index.php?taal=$1&page=$2&title=$3&beginnenbij=$4 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /index.php?taal=$1&page=$2&title=$3 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?taal=$1&title=$2 [L]

and otherwise:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?page=$1&title=$2&beginnenbij=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&title=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ /index.php?title=$1 [QSA,L]

how do I check this in my htaccess file for an right url build-up?

1

There are 1 answers

0
MrWhite On

You only need one set of rules. (Providing you are OK to have an "empty" taal URL parameter when the language code is omitted - you should be.)

([^/]*) - Rather than check for anything in the first (language code) path segment, you need to be more specific and either check for the specific (2-char?) language codes you are expecting, eg. (nl|en|id), or check for any lowercase 2-char path segment, eg. ([a-z]{2}), providing that does not conflict with a page? And then make the first path segment optional.

For example, a single set of rules to handle both cases:

RewriteRule ^(?:([a-z]{2})/)?([^/]+)/([^/]+)/([^/]+)/$ /index.php?taal=$1&page=$2&title=$3&beginnenbij=$4 [L]
RewriteRule ^(?:([a-z]{2})/)?([^/]+)/([^/]+)/$ /index.php?taal=$1&page=$2&title=$3 [L]
RewriteRule ^(?:([a-z]{2})/)?([^/]+)/$ /index.php?taal=$1&title=$2 [L]

Note that the first path segment (including the first slash) is made optional with the trailing ? and this group in non-capturing (as denoted by the (?: prefix. However, the inner group that captures the language code is capturing. When the language code is omitted (ie. the first path segment is not lowercase 2-chars) then the $1 backreference is simply empty, but is still present, so the following backreferences are not renumbered.

Note also that I changed the quantifier from * to + for subsequent path segments. It doesn't make much sense to use * here, which makes it "look-like" it allows an empty path-segment, eg. /nl/foo//baz - which would never be matched by the first rule anyway since the double slash in the middle is "resolved away" in the URL-path that the RewriteRule matches against so the URL would fall-through to the rule that follows anyway.