Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite question

multiple rules and how it works

         

slykryk

5:50 am on Dec 23, 2007 (gmt 0)

10+ Year Member



I am new to mod_rewrite and I have started using it with my website.
On my news page i have a domain.com/news/topic/# rule and that works fine but i have another rule for different page numbers and that is domain.com/news/topic/#/page/# and that one does not work.

here are my rules for it.

RewriteEngine On
Options +FollowSymlinks -MultiViews
RewriteRule ^news/([^/]+)/?$ /news.php?urltitle=$1 [L]
RewriteRule ^news/page/([^/]+)/?$ /news.php?page=$1 [L]
RewriteRule ^content/([^/]+)/?$ /content.php?urltitle=$1 [L]
RewriteRule ^news/topic/([^/]+)/([^/]+)/?$ /news.php?tid=$1&topictitle=$2 [L]
RewriteRule ^news/topic/([^/]+)/([^/]+)/page/([^/]+)/?$ /news.php?tid=$1&topictitle=$2&page=$3 [L]

Any suggestions on getting the page one to work?

Marcia

6:04 am on Dec 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry no idea, I just started reading the book. ;)

All I do know is that I keep reading in this forum that you're supposed to put your most specific rules first. So if that's more specific than others, try putting the specifics first and see if that helps. To me it looks like it might be more specific than others that come before it (but I could be wrong).

What I'm referring to is the order of directives. Here's just one thread on it, there are a lot of others in this forum that you can search for, too:

[webmasterworld.com...]

[edited by: Marcia at 6:08 am (utc) on Dec. 23, 2007]

phranque

10:26 am on Dec 23, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the patterns show a "topictitle" field but your sample request urls don't show that.

"does not work" leaves much latitude.
if you want a specific answer, you should provide a more specific description of the response and an examplified request url for each response.

g1smd

10:51 pm on Dec 23, 2007 (gmt 0)

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



My biggest error in the early days of messing with this stuff, was to not clearly define exactly what it was that I wanted the code to do.

I often wrote or adapted some code and it did exactly what it was coded to do, but that was often not quite what I actually wanted or needed it to do.

Defining which URLs it is to work for, and which it is not be used for, is half of the battle.

slykryk

11:05 pm on Dec 23, 2007 (gmt 0)

10+ Year Member



RewriteRule ^news/topic/([^/]+)/([^/]+)/?$ /news.php?tid=$1&topictitle=$2 [L]

I want this one to work for http://www.example.com/news/topic/1/Topic_Title

RewriteRule ^news/topic/([^/]+)/([^/]+)/page/([^/]+)/?$ /news.php?tid=$1&topictitle=$2&page=$3 [L]

i want this one to work for http://www.example.com/news/topic/1/Topic_Title/page/#

the second one does not work.

[edited by: jdMorgan at 12:19 am (utc) on Dec. 24, 2007]
[edit reason] example.com [/edit]

phranque

12:00 am on Dec 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



does not work

which means exactly what?
no response?
no server log entry?
404?
blue screen?
smoke escapes from the server?

Marcia

12:24 am on Dec 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>smoke escapes from the server?

ROFL, never realized what a fun forum this is. :)

/Topic_Title/page/# is more specific than
/Topic_Title

So if more specific is supposed to be coded before less specific (more general), shouldn't it come before /Topic_Title in the coding order? Or am I missing something?

Here's a post about how the sequencing works, very simply and clearly explained. Even though it's for a different issue, the principle of sequencing is the same:

[webmasterworld.com...]

The Redirect directive uses prefix-matching. Therefore, your less-specific whole-directory Redirect directives will always be invoked because they come first in your posted code, and the more-specific per-file ones won't ever be invoked.

And as a very distinct rule of thumb:

Always order your redirects and rewrites from most-specific (longest URL-path) to least-specific.

[edited by: Marcia at 12:33 am (utc) on Dec. 24, 2007]

g1smd

2:34 pm on Dec 24, 2007 (gmt 0)

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



Marcia is correct. Welcome to this little corner of WebmasterWorld by the way.

If you have a piece of code that applies to A and a piece of code that applies to AB (longer!) and you put the "A" code first then it will run for both A and AB inputs. The code you wrote especially for AB never gets to run.

So you put the code for AB first, and it runs only when an input of AB triggers it, and then exits. An input of just "A" will not trigger it, so the server tries the next piece of code in case that one does, and in this case it does, so it then runs.

Most specific goes first.

phranque

1:21 am on Dec 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



sorry i didn't notice - welcome to WebmasterWorld [webmasterworld.com], slykryk!

Most specific goes first.

in general that is good practice but he does have the patterns end-anchored so that is not necessarily the problem here.

the log entry would be interesting.