views:

29

answers:

1

I currently use the follwoing code in my .htaccess file

RewriteEngine On
RewriteBase /

# Redirect languages
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

With that code, every time I type for instance, /en at the end of the URL, it redirects me to /?lang=en (loads the the English content from a PHP array):

For instance:

example/en redirects me to example/?lang=en while keeping example/en in the URL.

But I also have a thanks.php page and the code above clearly just work for the index.php page.

How can I make the rewrite rule to work for both index.php and thanks.php page?

A: 

The most straight-forward way is just to do this:

RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

RewriteRule ^thanks/(en|es|zh\-tw|zh\-cn)/?$ thanks.php?lang=$1 [L]

If you want to make it more general, you have the option of white-listing files, like this:

RewriteCond $1    ^(thanks)/$ [OR]
RewriteCond index ^(index)$
RewriteRule ^(.+/)?(en|es|zh\-tw|zh\-cn)/?$ %1.php?lang=$2 [L]

...where (thanks) would be a pipe-delimited list of the files you wanted to have this functionality, or you can just accept every request as a pass-through to an existing PHP page:

RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?[^/]?)/(en|es|zh\-tw|zh\-cn)/?$ $1.php?lang=$2 [L]
Tim Stone