views:

68

answers:

3

What I'm trying to achieve:

1) http://localhost/en/script.php?param1=random is mapped to http://localhost/script.php?param1=random&language=English

  • This has to work always.

2) http://localhost/en/random/text/here will be mapped to http://localhost/categories.php?term=random/text/here

  • This has to work if random/text/here is 404

What I have at the moment:

RewriteEngine on
RewriteCond substr(%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.+)$ categories.php?lang=English&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ee/(.+)$ categories.php?lang=Estonian&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fi/(.+)$ categories.php?lang=Finnish&terms=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ru/(.+)$ categories.php?lang=Russian&terms=$1 [L]

RewriteRule ^en/(.*) $1?lang=English [QSA]
RewriteRule ^ee/(.*) $1?lang=Estonian [QSA]
RewriteRule ^ru/(.*) $1?lang=Russian [QSA]
RewriteRule ^fi/(.*) $1?lang=Finnish [QSA]

What is the problem:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 

It's supposed to redirect to categories.php?lang=English IF /en/this/here/does/not/match/a/script. If I load an URL like en/index.php it will also get mapped to categories.php?lang=English because en/index.php does not exist.

What I've thought:

substr(%{REQUEST_FILENAME},3) would fix my problem (as currently /ee/index.php is literally mapped to /ee/index.php instead of just /index.php)

Unfortunately I couldn't find a way to manipulate strings :/

A: 

For substr problem, you could try absolute paths:

RewriteRule ^en/(.*) /$1?lang=English&%{QUERY_STRING}

or

RewriteRule ^en/(.*) http:/localhost/$1?lang=English&%{QUERY_STRING}

Also, I might be nitpicking, but wouldn't it be easier if you did the language evaluation in the PHP base on language codes, 404'ed non-existent languages and used

RewriteRule ^(.*?)/(.*) $2?lang=$1&%{QUERY_STRING}

Edit: depending on how many scripts you have, can't you do something like:

RewriteRule ^(.*?)/(script.php|other.php) $2?lang=$1 [QSA]

= have pipe-list files, that are accesible?

Adam Kiss
The substitute problem begins at RewriteCond %{REQUEST_FILENAME} !-f/RewriteCond %{REQUEST_FILENAME} !-dIt's supposed to redirect to categories.php?lang=English IF /en/this/here/does/not/match/a/script. If I load an URL like en/index.php it will also get mapped to categories.php?lang=English because en/index.php does not exist.
Peeter
i see i see......
Adam Kiss
A: 

the issue is that you are using the L flag. Which means that rule will be the last to be executed.

also

%{QUERY_STRING}

isnt necessary, add QSA and you will get all parameters added to the end of the url

try to do:

RewriteRule ^en/(.*) $1?lang=English [QSA]
Gabriel Sosa
`[L]` means a rule is the last to be executed in the same way that what you look for is always in the last place you look. The flag doesn't change evaluation order, if that's what you meant.
outis
in the way that @Peeter done it the language rules wont never be reached, simple.
Gabriel Sosa
+1  A: 

I take it the language code is what makes the URL map to a non-existant file. Switch the two steps, moving the language code to the query string first. This also has the added advantage of simplifying the keyword step to a single RewriteRule, since they no longer need to do two things at once.

RewriteRule ^en/(.*) $1?lang=English [QSA,DPI]
RewriteRule ^ee/(.*) $1?lang=Estonian [QSA,DPI]
RewriteRule ^ru/(.*) $1?lang=Russian [QSA,DPI]
RewriteRule ^fi/(.*) $1?lang=Finnish [QSA,DPI]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ categories.php?terms=$1 [L,QSA]
outis
That worked fine, but for some reason if the URL is http://localhost/en/sales/subcategory this is the arguments that get passed to catagories.php: array(2) { ["terms"]=> string(35) "sales/subcategory/sales/subcategory" ["lang"]=> string(7) "English" } Any thoughts?
Peeter
Adding L to the language rules seems to have fixed that.
Peeter
@Peeter: The [L] flag will skip the "categories" rule. For URLs that don't map to files, the trailing path component is stored as path info. Apache might be doubling the component in an attempt to preserve the path info. Try adding the discard path info flag ("DPI") to the language rules (see updated code).
outis