Peeter
Peeter

Reputation: 9382

mod_rewrite and pretty urls

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

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

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 :/

Upvotes: 0

Views: 1052

Answers (3)

outis
outis

Reputation: 77400

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]

Upvotes: 1

Adam Kiss
Adam Kiss

Reputation: 11859

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?

Upvotes: 0

Gabriel Sosa
Gabriel Sosa

Reputation: 7956

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]

Upvotes: 0

Related Questions