Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set language using htaccess URLrewrite

I have for example two files in my root directory: one.php and two.php. I would like to reach them through these urls without having to actually have the physical directories en+de.

  • mydomain.com/en/one
  • mydomain.com/en/two
  • mydomain.com/de/one
  • mydomain.com/de/two

So the first directory would be "ignored" and just pass a GET variable with the language setting to the php-file, second directory is the php-file without extension.

This is what I'm using in my htaccess to loose the file extension so far:

RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-d   
RewriteCond %{REQUEST_FILENAME}\.php -f   
RewriteRule ^(.*)$ $1.php

UPDATE/SOLUTION
This is quite useful: .htaccess rule for language detection
and this seems to pretty much do what I wanted:

RewriteRule ^(en|de|fr)/(.*)$  $2?lang=$1 [L,QSA]   
RewriteRule ^(.*)$  $1?lang=en [L,QSA]

ONE MORE ADDITIONAL QUESTION
For mydomain.com/en/one/id45
If I wanna pass id45 as a variable to one.php what line in htaccess do I have to add?

like image 304
Tom Senner Avatar asked Sep 23 '13 13:09

Tom Senner


1 Answers

You can try this rule:

DirectoryIndex index.php index.html
DirectorySlash On

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$2.php -f
RewriteRule ^(en|de|fr)/([^/]+)(?:/(.*)|)$ $2.php?lang=$1&var=$3 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-d   
RewriteCond %{REQUEST_FILENAME}.php -f   
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/?$ index.php?lang=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1?lang=en [L,QSA]
like image 94
anubhava Avatar answered Nov 13 '22 19:11

anubhava