homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

How to apply combinations in one rule?

 12:18 pm on Jan 2, 2013 (gmt 0)

In my below written rule i want to use for-sale-mapjt-([^\ ]+)-cmty-([^\ ]+)-tpe-([^\ ]+) where each below part is optional but order will stay same.

(1) -mapjt-([^\ ]+)
(2) -cmty-([^\ ]+)
(3) -tpe-([^\ ]+)

My Rule:
RewriteRule ^([^\ ]+)/for-sale-mapjt-([^\ ]+)-cmty-([^\ ]+)-tpe-([^\ ]+)/$ /myfilename.php?mod=$1&project=$2&subproj=$3&projtype=$4 [L]

My rule doesn't work as in my rule all parts are mandatory. I need help how can i make each part optional so that if either part doesn't exist rule should still work intead of making lot of rules for each combination.



 12:45 pm on Jan 2, 2013 (gmt 0)

Hi sam,

I'm not sure that's possible i'm afraid as whilst the regexp in which each section is optional is straight forward, the back references ($1, $2 etc.) would change.

The alternative is simply to process $_SERVER["REQUEST_URI"] in your script and extract each variable as required. This would use a single rule, e.g.

RewriteRule ^([^\ ]+)/for-sale(.*)$ /myfilename.php [L]

Then at the top of myfilename.php, $_SERVER["REQUEST_URI"] will contain something like;


..from which you could extract your variables as follows:

$mapjt = $matches[1];
$cmty = $matches[1];
$tpe = $matches[1];


 12:58 pm on Jan 2, 2013 (gmt 0)

([^\ ]+)
What the bleep bleep? An URL will never contain literal spaces* so what's this set supposed to capture? Or rather, what's it meant to exclude? There's no point in excluding something that would never occur anyway: it's just like saying .+ which is Not A Good Idea. Especially not three separate times, two of them non-final.

It will be much more useful if you give some sample URLs, or explain your naming pattern, so we can see what kinds of material come in those mystery areas:


Once you know what kind of text can and can't occur in those places, you can start on a workable rule. I assume it's something more complicated than [^-]+ or you would have said so.

And if you mean that the whole components like
are optional, then your rule needs a lot more parentheses and question marks.

* Unless, ahem, you have been Very Unwise in your naming.


 1:23 pm on Jan 2, 2013 (gmt 0)

Thanks for the replies. dmorison method helps :)


 8:53 pm on Jan 2, 2013 (gmt 0)

the back references ($1, $2 etc.) would change

Back references look at definitions, not captures. So


should always come through as $1$2$3 even if the content of some captures is null. The tricky part in this one was nesting:


and so on.


 9:06 pm on Jan 2, 2013 (gmt 0)

The logic "not spaces, followed by a hyphen" is fatally flawed.

The "not spaces" capture will capture the text, but it will also eat the hyphen and the next text and the next hyphen onwards to the end.

This happens as the text is "not a space" and the hyphen is "not a space".


 1:37 pm on Jan 21, 2013 (gmt 0)

As by this: RewriteRule ^([^\ ]+)/for-sale(.*)$ /myfilename.php [L]
it works well. Is there any way to make server keep the order of parameters and redirect to original url if order gets changed like if someone tries with:

then it should be redirected to its original URL:


 2:46 am on Jan 22, 2013 (gmt 0)

Is there any way

There are several ways, depending on how many parameters are involved and what happens behind the scenes.

Probably the easiest and most practical way is to let your existing php script take care of it. Before the php starts processing the request, let it look at the parameters. If they're in the wrong order, put them in the right order and issue a redirect. Your php is a lot more flexible than mod_rewrite and can do more complicated calculating and rearranging.

That's assuming it is your own php, not an out-of-the-box CMS that you can't edit. Otherwise it is back to mod_rewrite. Or over to a separate php script which you do write yourself.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved