Lippi
Lippi

Reputation: 13

mod_rewrite with optional variable at the start of the URL

I want to setup mod_rewrite to handle multiple languages but I can't get it to work properly.

I want the following to work:

example.com/en/page/abc -> example.com/page.php?language=en&id1=abc example.com/en/page/abc/def -> example.com/page.php?language=en&id1=abc&id2=def

My .htaccess file looks like this:

    RewriteEngine On

    RewriteRule ^(.*)/page/(.*)/(.*)  page.php?case=a1&language=$1&id1=$2&id2=$3
    RewriteRule ^(.*)/page/(.*)  page.php?case=a2&language=$1&id1=$2

Now if I enter the url 'en/page/abc' it works fine, but if I enter 'en/page/abc/def' it executes the second rule and gives 'page.php' as language and 'abc/def' as variable for id1. Reversing the order still gives 'page.php' as value for language. Anyone a suggestion how to fix this?

Upvotes: 0

Views: 82

Answers (1)

Lippi
Lippi

Reputation: 13

Replacing the first (.*) with ([a-z]+) did the trick.

My .htaccess now look like this:


    RewriteRule ^([a-z]+)/page/(.*)/(.*)        page.php?language=$1&id1=$2&id2=$3
    RewriteRule ^([a-z]+)/page/(.*)             page.php?language=$1&id1=$2

Upvotes: 0

Related Questions