Forum Moderators: phranque

Message Too Old, No Replies

Need help with mod rewrite issues

Going to the wrong page.

         

neebster

4:58 pm on Oct 26, 2011 (gmt 0)

10+ Year Member



Hi,

I hope that you can help. I have web site which I would like to use friendly URL's and I've got it pretty much working, however there are a couple of pages which aren't displaying correctly.

My .htaccess file has this code:


php_flag display_errors on
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 /notfound.php
RewriteRule ^news/department/([^/\.]+)/?$ /news/department/index.php?cat=$1 [L]
RewriteRule ^news/year/([^/\.]+)/?$ /news/year/index.php?year=$1 [L]
RewriteRule ^news/([^\.]+)/?$ /news/news.php?newsURL=$1 [L]


The folder and file structure is:


news
index.php
news.php
department
index.php
year
index.php


Example URL's that are working the way I expect would be:

www.mywebsite.com/news/ => goes to /news/index.php
www.mywebsite.com/news/title-of-news-article => goes to /news/index.php?newsURL=title-of-news-article
www.mywebsite.com/news/year/2010 => goes to /news/year/index.php?year=2010

However when I go to the URL www.mywebsite.com/news/year the page displayed is actually /news/news.php and because there is no parameter it shows various error messages. The same happens when going to /news/department/. How can I get it to show the /news/year/index.php page?

I feel sure that there is something that I can change in the rules or do them in a different way, however I have no idea what that could be. Any help you can provide would be much appreciated.

lucy24

9:17 pm on Oct 26, 2011 (gmt 0)

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



Leave a blank line after each RewriteRule. It will keep you sane. RewriteBase / is almost never necessary, because it's the default.

However when I go to the URL www.mywebsite.com/news/year the page displayed is actually /news/news.php

Yes, that's what you've told it to do. Rule #2 can't apply, because there is no further text after the word "year", so we proceed to Rule #3

RewriteRule ^news/([^\.]+)/?$ /news/news.php?newsURL=$1 [L]


There should be a parameter, though: newsURL=year

If the php is expecting a number and you haven't coded for parameters in unexpected formats-- which you should always, always do in any case-- that gets you an error.

Rules 1 and 2 both specify / followed by more text, meaning that they can only apply if they end in "year/{blahblah}" or "department/{blahblah}".

Uhm. I just realized that I answered a functionally identical question within the past hour. Have a look through the most recent threads in this Forum.

neebster

9:50 pm on Oct 26, 2011 (gmt 0)

10+ Year Member



Thanks lucy24.

I have removed the RewriteBase line.

I understand now what is happening. I have added:


RewriteRule ^news/year/$ /news/year/index.php [L]


underneath the news/year/([^/\.]+)/?$ line. This is now displaying the correct page, however not quite the correct content I need, but that's a different issue.

Many thanks.

g1smd

11:26 pm on Oct 26, 2011 (gmt 0)

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



In a character group, the period should not be escaped.

The optional trailing slash /?$ promotes duplicate content. You should code this so that URLs without a trailing slash and without an extension are rewritten, and URLs with a trailing slash are redirected (using another extra rule) to remove that trailing slash.