Htaccess mod_rewrite - problem with ending slash - "/"

Hello,

I have setup mod_rewrite in my .htaccess to have my pages more user friendly.

So, I have a user friendly url : example.com/news

The problem is that if I add a slash at the end, such as example.com/news/ then it does not work…

I have two rules, one for the first part, which yields results such as:
example.com/news
example.com/news2
example.com/news3
etc

and a 2nd rule, which digs down more:
example.com/news/article1.html
example.com/news/article2.html
example.com/news/article3.html
etc.

So, everything is fine if I type this:
example.com/news
example.com/news2
example.com/news/article2.html
example.com/news/article3.html

The only problem is that this is not working, with the slash at the end:
example.com/news/
example.com/news2/
example.com/news3/

Here is my code, I don’t know what I am doing wrong:

RewriteRule ^news(.*) index.php?page=news$1 [L]
RewriteRule ^news/(.*).html$ index.php?page=articles&file=$1 [L]

Adam,

For your own sake, please learn the difference between directory and directory/ and directory/file. directory forces Apache to look for a filename called directory and, upon not finding one, then looks for directory/, the URI for the directory. Obviously directory/file is the file within the directory.

What’s this have to do with mod_rewrite and regex? mod_rewrite looks at the {REQUEST_URI} string and attempts to match that (with the regex of the RewriteRule or the regex of a RewriteCond when %{REQUEST_URI} is specified). When you add or remove that trailing slash, you’re changing the directory level of the URI and THAT will cause you a problem (because you are forced to use absolute redirections).

My solution to this is to use link to directories (directory/) when I want that directory’s DirectoryIndex to be served or to a path/file when I want that file to be served. If anyone wants to “play games” on my sites, I can’t necessarily stop them from using “stupid links” but I can prevent them from getting anything more than a 404 response (which is more than they deserve).

That makes my response: Don’t bother with it, it’s not worth the effort.

Regards,

DK