munge83
munge83

Reputation: 153

.htaccess RewriteRule when empty string

I'm trying to do something like this http://myweb.com//page

If first parameter is not present it process only the second parameter. the first is language and its stored in the session but , it can be changed throe ?lang=en..

I was trying something like this but it's not working

RewriteRule ^([a-zA-Z_]+)?/([a-zA-Z0-9_-]+)$ index.php?lang=$1&page=$2

Upvotes: 1

Views: 2425

Answers (2)

Question Mark
Question Mark

Reputation: 3606

On a previous language-switchable site i did, i opted for always having the language in the url, it was easier to implement all round and looked better to the user.

RewriteRule ^$ /en/ [L]   #default language
RewriteRule ^([^/]+)/(.+)$ index.php?lang=$1&page=$2 [QSA,NE]   # first "slug" is presumed always language the rest is a page identifier

Here instead of directing to /en/ you could direct the user with no language set, to a geoip script or a script that chops up the user agent string for a language code or something to make a guess more accurate.

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143906

Try making them 2 separate rules, because the leading slash is removed.

RewriteRule ^([a-zA-Z_]+)/([a-zA-Z0-9_-]+)$ index.php?lang=$1&page=$2 [L,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L,QSA]

Upvotes: 2

Related Questions