views:

286

answers:

1

I have my site broken down into several folders by language:

/
  /en/
    index.php
    about-us.php
    faq.php
    ...

  /fr/
    index.php
    about-us.php
    faq.php
    ...

  ...
  etc.

I'd like to have a rewrite rule that automatically rewrites to the en folder if somebody tried to enter mydomain.com/about-us.php.

FYI, I also already have a rewrite rule in place that removes the extension, so really I want to make sure that mydomain.com/about-us rewrites to mydomain.com/en/about-us. Here's my existing rewrite rule that does this:

# allows for extension-agnostic urls 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

That make sense? Anyone help me out here?

EDIT:

@Gumbo -

Here's what my .htaccess file looks like (this is all that's in it):

RewriteEngine On

# defaults to the english site
RewriteRule !^[a-z]{2}/ /en%{REQUEST_URI} [L,R=301]

# allows for extension-agnostic urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

This file is in the root of my website (not in a language folder). I've reloaded my webserver, just to be on the safe side, and I still get this error message when I try to go to mydomain.com/about-us:

Not Found

The requested URL /about-us was not found on this server.

... where mydomain.com/en/about-us works just fine.

Any other thoughts?

+1  A: 

Put this rule before your rule:

RewriteRule !^(fr|en)/ /en%{REQUEST_URI} [L,R=301]

Or more general:

RewriteRule !^[a-z]{2}/ /en%{REQUEST_URI} [L,R=301]
Gumbo
I prefer the second but I guess it depends on the number of languages. +1
cballou
Neither seems to be working for me. I put your rule--verbatim--after my two rewrite conditions and before my existing rewrite rule, but no joy (file not found). Is there something I'm missing?
neezer
@neezer: Where do you want to use that rule?
Gumbo
@Gumbo: Well, I'd like to use it where ever it will work. I have it now in the .htaccess file in my site's root.
neezer
@neezer: Well, it should work for your .htaccess file in the document root.
Gumbo
@Gumbo - See my edit above.
neezer