Fabián
Fabián

Reputation: 575

More than one rewriterule

The idea is to get different variables depending of which php file I'm including, so I avoid too much confusion when programming.

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l

    RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9+]+)$ index.php?p=$1&produccion=$2 [QSA,L]
    RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9+]+)$ index.php?p=$1&categoria=$2 [QSA,L]
    RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9+]+)/([a-zA-Z0-9+]+)$ index.php?p=$1&categoria=$2&show=$3 [QSA,L]
</IfModule>

Thing is, when I leave it like this, it will overwrite the previous RewriteRule.

I'm really new to this regex and apache thing.

Upvotes: 0

Views: 99

Answers (1)

theftprevention
theftprevention

Reputation: 5213

You might consider using just one RewriteRule:

RewriteRule ^(.+)$ index.php?p=$1 [QSA,L]

And then parsing the $_GET['p'] variable in PHP to determine the appropriate page:

$page = (isset($_GET['p'])) ? explode('/', $_GET['p']) : null;
if ($page) {
    if ($page[0] == 'store') {
        if ($page[1] == 'cart') {
            // URL is www.example.com/store/cart
        }
        else if (!isset($page[1]) || empty($page[1])) {
            // URL is www.example.com/store
        }
    }
}
else {
    // URL is www.example.com
}

This isn't exactly ideal for your site's infrastructure. You also might consider being more specific with your RewriteRules if you don't want absolutely everything to be redirected. Take this for example:

RewriteRule ^(about|faq|signup|contact)(?!\.php)/?(.*)$ /$1.php?page=$2 [QSA,L]

The above rule will only redirect when a user visits example.com/about, or /faq, or /signup, or /contact, and they'll be redirected to a PHP file of the same name. For example, requests to /faq/3 will redirect to /faq.php?page=3.

Upvotes: 1

Related Questions