4
votes

I have a bit of a dilemma with mod_rewrite.
How do I write a rule that will decide the following:

If i browse /en/page/page I want to rewrite this to:
index.php?language=en&page=/page

BUT if i browse just /page I want to rewrite this to:
index.php?page=/page

Is this possible?

Edit:
/page is just a random dynamic pagename, it could aswell be whatever.

I need a rule that checks like /se == a string, and not longer than 2 characters>/page --> index.php?lang=se&page=/page and if browsing page /page it will notice that /page == longer than 2 characters and rewrite to index.php?page=/page

Edit2: Found the anser to my question:


    RewriteEngine On
    RewriteBase /

    ## Check if its not a filename or dirname
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    ## Rewrite rules
    RewriteRule ^(.{2})/(.*)$ index.php?lang=$1&page=$2 [L]
    RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

3

3 Answers

3
votes

I got it to work on my own! Here's the rules if someone else needs to make the same thing:

    RewriteEngine    On
    RewriteBase    /

    ## Check if its not a filename or dirname
    RewriteCond %{REQUEST_FILENAME}    !-l
    RewriteCond %{REQUEST_FILENAME}    !-f
    RewriteCond %{REQUEST_FILENAME}    !-d

    ## Rewrite rules
    RewriteRule ^(.{2})/(.*)$    index.php?lang=$1&page=$2 [L]
    RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
0
votes
RewriteRule ^page$ index.php?language=en&page=/page/page [L]
RewriteRule ^page/ index.php?language=en&page=/page [L]
0
votes

try this:

RewriteRule ^(.*)/(.*)(/.*)$ index.php?language=$1&$2=$3 [L]
RewriteRule ^page$ index.php?page=/page/page [L]