Forum Moderators: phranque

Message Too Old, No Replies

rewrite help

         

Shawn05

11:34 pm on Sep 30, 2009 (gmt 0)

10+ Year Member



I am new to this and I am trying to get it to work the way I need it. I can get it to work fine but the problem is I will not always have the same number of variables.

Below is what I have
rewriterule ^article/([^&]+)/([^&]+)$ index.php?article_id=$1&article_name=$2 [L]

Which works fine with this in the url "article/1234/test_article"

But what I need is to see if there is another variable passed after the article name. And if there is I need to grab another.

Any help would be great! thanks!

jdMorgan

12:30 am on Oct 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Simply write another rule to handle that case...

Also, if you're not sure what "[^&]+" means, I suggest you look into it, because that pattern seems inappropriate for use in your described application. Indeed, if you've tried writing a second rule and have had problems with the two rules working together in a particular order, this is the likely cause.

There's a concise regular-expressions tutorial cited in our Forum Charter (ee link at top of this page).

Jim

Shawn05

6:42 pm on Oct 1, 2009 (gmt 0)

10+ Year Member



Thanks Jim I think I got what I needed with the tutorial/guide you gave me!

jdMorgan

6:51 pm on Oct 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good!

Feel free to post your new rules for review.

Jim

Shawn05

7:29 pm on Oct 1, 2009 (gmt 0)

10+ Year Member



The actual variables are subject to change as this is just a test. I ended up with four. I have tested them all and they work.

rewriterule ^article/([^&]+)/([^&]+)/([^&]+)/article_title/([^&]+)$ index.php?article=$1&module=$2&id=$3&article_title=$4 [L]
rewriterule ^article/([^&]+)/([^&]+)/article_title/([^&]+)$ index.php?article=$1&module=$2&article_title=$3 [L]
rewriterule ^article/([^&]+)/([^&]+)/([^&]+)$ index.php?article=$1&module=$2&cmd=$3 [L]
rewriterule ^article/([^&]+)/([^&]+)$ index.php?article=$1&module=$2 [L]

jdMorgan

2:05 am on Oct 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I warned about that "[^&]" pattern above... Looks like you didn't dig into that.

I would suggest changing those patterns, for example, making your first rule look like this:


RewriteRule ^article/([^/]+)/([^/]+)/([^/]+)/article_title/(.+)$ index.php?article=$1&module=$2&id=$3&article_title=$4 [L]

That might run, oh, about 128 times faster... :) Actual gains depend heavily on the length of the requested URL-path, but that's a fair guess; It could only be 20 times faster, or it could be 10,000...

Apply that same approach to your other rules as well; Note that the last sub-pattern of the four in the example above is intentionally different.

Using 'the right regular expressions for the job' at hand is important; Don't ignore this aspect of using mod_rewrite.

Jim

Shawn05

3:22 am on Oct 2, 2009 (gmt 0)

10+ Year Member



Made the changes. Everything still works so all is good. Thank you again!

g1smd

7:35 am on Oct 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Everything still works so all is good.

Always test with a range of expected and unexpected URL requests, and measure the responses using "Live HTTP Headers".