Multilanguage website mod rewrite problem
-
11-01-2021 - |
Question
I have a problem with mod rewrite and did not find any solution here. Here is the problem:
I have website with two languages and mod URL should look something like this:
/eng/contact
/srp/kontakt
/eng/news
/srp/vesti
/eng/event
/srp/najava
Mine rewrite rule is not working because I have in .htacess situation like this:
# news
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ news.php?lang=$1&pagename=$2 [NC,L]
# contact
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ contact.php?lang=$1&pagename=$2 [NC,L]
# event
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ event.php?lang=$1&pagename=$2 [NC,L]
My question is how to achieve rewrite for pages in the above examples?
No correct solution
OTHER TIPS
I would use:
RewriteRule ^([^/]+)/([^/]+)/*$ index.php?lang=$1&pagename=$2&%{QUERY_STRING}
and then route the PHP flow from index.php to news.php/contact.php etc. by using some simple switch-case-include statement:
switch ($_GET['pagename'])
{
case 'news':
require_once 'news.php';
break;
...
...
}
This will also help you develop other routing related features simplifying the .htaccess file. This also enables easy lookup for native subpages names of subpages like "en/contact" but "pl/kontakt" etc.
I use this approach on almost all my sites (e.g. http://www.calculla.com/en/ascii2hex and http://www.calculla.com/pl/ascii2hex).