Welcome to WebmasterWorld Guest from 54.226.27.104

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

How to apply combinations in one rule?

     

sam222

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.

dmorison

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

WebmasterWorld Senior Member 10+ Year Member



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;

/for-sale-mapjt-FOO-cmty-BAR-tpe-OOF

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


preg_match('/mapjt-(.*)[\-]/U',$_SERVER["REQUEST_URI"]."-",$matches);
$mapjt = $matches[1];
preg_match('/cmty-(.*)[\-]/U',$_SERVER["REQUEST_URI"]."-",$matches);
$cmty = $matches[1];
preg_match('/tpe-(.*)[\-]/U',$_SERVER["REQUEST_URI"]."-",$matches);
$tpe = $matches[1];

lucy24

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



([^\ ]+)
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:

for-sale-mapjt-{stuff}-cmty-{stuff}-tpe-{stuff}

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
-mapjt-{stuff}
are optional, then your rule needs a lot more parentheses and question marks.


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

sam222

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



Thanks for the replies. dmorison method helps :)

lucy24

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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

Back references look at definitions, not captures. So

(stuff)?(morestuff)?(stillmorestuff)?

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:

(?:blah-(capture))?(?:-moreblah-(morecapture))?

and so on.

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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".

sam222

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:
/for-sale-tpe-OOF-mapjt-FOO-cmty-BAR

then it should be redirected to its original URL:
/for-sale-mapjt-FOO-cmty-BAR-tpe-OOF

lucy24

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month