Forum Moderators: phranque
www.site.com/news/?topic=topic_name&news_id=123&title=exampletitle 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.
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]
Jim
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)