Tom Senner
Tom Senner

Reputation: 548

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.

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?

Upvotes: 7

Views: 5251

Answers (1)

anubhava
anubhava

Reputation: 785156

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]

Upvotes: 2

Related Questions