4

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]

0

3 Answers 3

3

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]
Sign up to request clarification or add additional context in comments.

Comments

0
RewriteRule ^page$ index.php?language=en&page=/page/page [L]
RewriteRule ^page/ index.php?language=en&page=/page [L]

Comments

0

try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.