Take the 2-minute tour ×

Ok, so basically I have this part of the .htaccess partly working, but not quite fully just yet. I have two subdirectories on my server for an english and a french site (the subdirectories are en, and fr, respectively). When a user enters something in the URL bar I want whatever they typed in to be added to en or fr (depending on whatever the user's default language is on their computer).

If they type in domain.com/test (and their language is set to english), I want it to redirect to domain.com/en/test/. I ONLY want this to happen if test is not a folder, directory, file, or ANYTHING that is in the root directory.

Here is my .htaccess code so far that kind of works:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^(.*)$ /en/$1/ [R=301,L]

The problem with my code though, however, is that if a user entered in something like domain.com/test/ (WITH the slash), it will not work because I have another part in my .htaccess that forces a slash on the end of everything. If a user entered in domain.com/test.html (and that is a file in the root of the domain), it will not work because I have another part in my .htaccess that removes .html extensions.

Basically, I need this redirect to work in absolutely all situations except where it logically shouldn't.

Anyways, if any of you could please help me out I would really appreciate it.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

OK. This is how I do it:

Always require the language suffix /en, /fr etc no matter what. So set that as a URL GET. With PHP you can force it to be inserted.

RewriteRule ^([a-z]{2})/?$ index.php?language=$1
RewriteRule ^([a-z]{2})/register/?$ register.php?language=$1

With PHP or whatever scripting you use, you can check the IP, set the country GET automatically, &/or set a session. build the session into all your URLS, so all your links have


Make the slash optional with /? as in above examples.

I do not use {REQUEST_FILENAME}, instead i have a list of all the possible main urls - but you can use {REQUEST_FILENAME} as well if you like, but you will need to account for real directories.

share|improve this answer
I do not want to use PHP at all, I am only interested in using .htaccess file. Like I said in the OP, I have it partly working. It adds /en/ if the URL entered doesn't have a slash on the end. But it also rewrites the URL in some case where I don't want it to... Such as: if the file is already in the root folder. –  shootingrubber Apr 3 '12 at 8:03
There's also another problem with my original code as well. When I am actually in the en/ subdomain, it adds another en/ onto the URL as well. So if I am on domain.com/en/test, and then I click on the example, it will go to domain.com/en/en/example. –  shootingrubber Apr 3 '12 at 8:10
Thanks, it helped me to. –  Elkas Dec 20 '12 at 17:11
add comment

Your Answer


