Multiple rewrite rules, ignored when file is found

New to mod_rewrite? This is a good place to start.

Multiple rewrite rules, ignored when file is found

Postby blister » Fri Oct 23, 2009 1:02 am

I want to create the following rewrite:

dir1-dir2-dir3-file-language.html

to

index.php?l=dir1:dir2:dir3&target=file&lang=language

It works with the following rules:

Code: Select all
RewriteRule ^([a-z0-9\-]+)-([a-z0-9]+)-([a-z0-9]+)\.html?$ temp/l=$1&a=$2&lang=$3
RewriteRule ^temp/(.*)-(.*)$ temp/$1:$2
RewriteRule ^temp/l=([a-z0-9:]+)&a=([a-z0-9]+)&lang=([a-z0-9]+)$ index.php\?l=$1&a=$2&lang=$3.html            


First rule makes sure the parameters l, a and lang are set
the second rule replaces all - with :
the third rule links this url to index.php

I am new to mod_rewrite, and I've got a gut feeling I am doing it wrong :D It bugs me that I have to rewrite the URL to temp/... I'd rather do something like this:

Code: Select all
RewriteRule ^([a-z0-9\-]+)-([a-z0-9]+)-([a-z0-9]+)\.html?$ index.php?l=$1&a=$2&lang=$3
RewriteRule ^index\.php\?(.*)-(.*)$ index\.php\?$1:$2


But then, the Rewriting stops after the first rule. It forms the url index.php?l=dir1-dir2-dir3&a=file&lang=language. The '-' isn't replaced with ':' no more.

Am I missing something here?
blister
 
Posts: 1
Joined: Fri Oct 23, 2009 12:49 am

Postby richardk » Fri Oct 23, 2009 9:15 am

Use str_replace() in PHP, mod_rewrite isn't good at search and replace.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([-a-z0-9]+)-([a-z0-9]+)-([a-z0-9]+)\.html?$ index.php?l=$1&a=$2&lang=$3 [QSA,L]

Code: Select all
$_GET['l'] = $_REQUEST['l'] = str_replace(':', '-', $_GET['l']);
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 9 guests

cron