|How to apply combinations in one rule?|
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-([^\ ]+)
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.
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;
$cmty = $matches;
$tpe = $matches;
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.
Thanks for the replies. dmorison method helps :)
|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.
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".
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:
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.