Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite and multiple variable query string

mod_rewrite gives 404s if all vraibles aren't present

         

fwordboy

2:13 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



I'm using mod_rewrite to convert
www.site.com/news/?topic=topic_name&news_id=123&title=exampletitle

into
www.site.com/news/topic/123/exampletitle

the mod_rewrite i'm using is

RewriteEngine On
RewriteRule ^(.*)/(.*)/(.*)$ index.php?topic=$1&news_id=$2&title=$3

This shows the article (only if all the variables are present) but i get 404 errors when I try to access

site.com/news/topic/
if I change the mod_rewrite code to
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?topic=$1&news_id=$2

Then I can view the

site.com/news/topic/
but the different pages display the same article. The third variable in the query string is there for pure usability alone, it doesn't influence the page, so it can be removed from the mod_rewrite I suppose. With the latter mod_rewrite code, when I put on the page a
<?php echo $_GET['topic'];?>
I am shown the topic name with a slash after it - so the mod_rewrite is rewriting to this:
www.site.com/news/?topic=topic_name/&news_id=123&title=exampletitle

any ideas? I'm a mod-rewrite novice so any clues would be much appreciated.

jdMorgan

4:23 pm on Dec 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is another example of why I advise against the use of the ".*" pattern when possible. As you observed, ".*" will match *anything* and it will match as much as possible. You will find references to ".*" as the "greediest" pattern in most regular-expressions tutorials (see our charter for a reference citation).

Unfortunately, it is also the "easiest" regex pattern to use, and is "comfortable" because it resembles the "*.*" wildcard pattern familiar to many from MSDOS'es and other operating systems' file-search function. However, its use can have unexpected side-effects, and this is one of them.

The proper way to do what you want is to use a forward-looking negative match, and to explicitly handle the title- and non-title cases:


RewriteRule ^([^/]+)/([^/]+)/(.+)$ /index.php?topic=$1&news_id=$2&title=$3 [L]
RewriteRule ^([^/]+)/(.+)$ /index.php?topic=$1&news_id=$2 [L]

Each "([^/]+)/" subpattern requires something-not-equal-to-a-slash followed by a slash, followed by the rest of the pattern. This type of pattern may be harder for us to write and read, but it is much more specific and much faster to process.

Jim

fwordboy

4:44 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



thankyou thats much better. I've had to modify it to suit my needs better.

RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/(.+)$ index.php?topic=$1&news_id=$2&title=$3 [L]
RewriteRule ^([^/]+)/$ index.php?topic=$1 [L]

But this only allows the works with the trailing slash at the end of the topic. If someone comes along to their browser and types in

www.site.com/news/topic
without the trailing slash they will get a 404. It also doesn't allow
ww.site.com/news/topic/123/
gives a 404 (with or without trailing slash)