0

I am a bit puzzled about mod-rewrite and php and need your help please.

Right now, I have a simple htaccess rule as follows:

RewriteRule ^en/(.*)$ /$1 [QSA,L]
RewriteRule ^de/(.*)$ /$1 [QSA,L]

How can I check if "en" or "de" or nothing has been defined as the first "part" after documentroot (www.mydomain.net/en oder www.mydomain.net/de or www.mydomain.net) in order to include the appropriate language file then by using:

include_once(dirname(__FILE__) . "/../languages/lang." . $lang . ".php");

I already played around with request URI and dirname but I couldn't get it working so far.

In addition to that: How would I need to append the mod-rewrite rule in order to also support urls like www.mydomain.net/en/admin/settings ?

Any kind of help is much appreciated, thanks...

Quhalix89
  • 129
  • 1
  • 2
  • 10
  • 2
    ^(en|de)/(.*)$ do like this in the begining and you can catch it with $1, and $2 – Muhammed Mar 13 '16 at 17:04
  • Hi, what do you mean "you can catch it with $1 and $2"... sorry, but I am not that familiar with mod-rewrite... – Quhalix89 Mar 13 '16 at 17:27
  • 1
    Use the [`explode()`](http://php.net/manual/en/function.explode.php) function to split the URI parts. The first item in the array would be your language code. – Mike Rockétt Mar 14 '16 at 05:44

2 Answers2

1

In your htaccess have this:

RewriteEngine On    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(de|en)/(.*)$ /index.php?lang=$1&route=$2 [L,QSA]

In index.php, catch them like this:

$lang = $_GET['lang']; // echo $lang, prints 'en' or 'de'
$route= $_GET['route']; //echo $route prints everything after slash.

Don't forget to reference all your CSS and image files relative to webroot as "/includes/css.css"

$route is going to be empty for your homepage http://example.com/

Example URL: http://example.com/de/admin/settings

$lang will be de and $route will be admin/settings

Hope this helps.

Muhammed
  • 1,592
  • 1
  • 10
  • 18
0

first of all: thanks for your help, it pointed me to the right direction.

When someone opens one of my urls, I save the entire (request) url to a variable using the following code: https://stackoverflow.com/a/10133524/989978

After that, I extract the language folder, as suggested by Mike Rockett, using explode(). However, I am doing it with the following function: https://stackoverflow.com/a/23856628/989978

Having that, I can easily include the appropriate language file and define a default fallback if nothing is provided. Lucky enough, it also gives me the ability to use urls with multiple subfolders, everything is working fine.

The next thing I have to think about is, how I could offer multi-language urls (en: .../user/settings vs. de: .../benutzer/einstellungen), but I learned a lesson again: Use the brain first, and then try to search stackoverflow ;-)

Thanks again guys.

Community
  • 1
  • 1
Quhalix89
  • 129
  • 1
  • 2
  • 10