7

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?

Community
  • 1
  • 1
Tom Senner
  • 548
  • 1
  • 6
  • 21

1 Answers1

2

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks, but it causes a "500 internal server error" maybe my other code is wrong: `RewriteEngine on`
    `RewriteCond %{REQUEST_FILENAME} !-d`
    `RewriteCond %{REQUEST_FILENAME}\.php -f`
    `RewriteRule ^(.*)$ $1.php`
    `RewriteRule ^(en|de|fr)/([^/]+)/(.*)$ $2.php?lang=$1&var=$3 [L,QSA,NC]`
    `RewriteRule ^(.*)$ $1?lang=de [L,QSA]`
    – Tom Senner Sep 24 '13 at 08:24
  • Your code works for mydomain.com/en/one/id45 but I'll get an 404 now for mydomain.com/en/one although the file one.php exists – Tom Senner Sep 24 '13 at 08:59
  • Thanks almost perfect: Only mydomain.com/en/, mydomain.com/fr/ etc. is giving 404-error instead of using the index.php – Tom Senner Sep 24 '13 at 09:23
  • But the directories DO NOT exist! mydomain.com/en/one -> will use one.php as a file so mydomain.com/en/ should use index.php in the same directory where one.php is. (Thanks for your great help btw) – Tom Senner Sep 24 '13 at 09:39
  • why is there multiple RewriteRule for the language? and why do it load the CSS if I type example.com/en/home but not if I type example.com/de/home – TheCrazyProfessor Apr 04 '17 at 08:33
  • @TheCrazyProfessor: Please post a new question with all the details and I will try to answer it. – anubhava Apr 04 '17 at 10:23