Forum Moderators: phranque

Message Too Old, No Replies

Extra trailing slash with mod_rewrite

The answer is here somewhere...

         

riverstyx

4:06 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



I'm using a link script with mod_rewrite in .htaccess (below). The trouble is that it is adding an extra slash to all urls: www mysite.com/links//index.php?etcetcetc and consequently the script doesn't work. Here's the rewrite -- I know the answer is here somewhere...any help appreciated.

Options +FollowSymlinks
RewriteEngine on
RewriteBase /links

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]

RewriteRule ^admin$ admin/index.php
RewriteRule ^admin/$ admin/index.php

RewriteRule ^links/([a-z]+)/([0-9]+)/?$ link.php?action=$1&id=$2
RewriteRule ^links/([0-9]+)/?$ link.php?id=$1

RewriteRule ^thread/([0-9]+)/?$ comments.php?id=$1
RewriteRule ^thread/([0-9]+)/([0-9]+)/?$ comments.php?id=$1&page=$2

RewriteRule ^([0-9]+)/?$ index.php?action=displaycat&catid=$1
RewriteRule ^([0-9]+)/([0-9]+)/?$ index.php?action=displaycat&catid=$1&page=$2

RewriteRule ^([-_/\+A-Za-z0-9]+)$ index.php?action=displaycat&catname=$1

RewriteRule ^links/([-_/\+A-Za-z0-9]+)-([0-9]+).html$ link.php?action=detail&id=$2

jdMorgan

5:00 pm on Aug 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your first rule will fail because the trailing "/" will be stripped, resulting in a match even if it was present.

RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.+) http://www.example.com/links/$1/ [R=301,L]

If this were my site, I'd look carefully at two things: First, the use of RewriteBase seems unnecessary, and is one of the factors that made the original code fail. Second, if you put this code in the /links directory itself, it will obviate the need for RewriteBase, and make your site run faster by avoiding the "directory exists" check on most files.

If RewriteBase is not used, the simple form is to specify the full local URL-path. I can't be sure this code will be correct, because I don't know how your site is structured, but an example for use in your top-level directory .htaccess would be:


RewriteRule ^links/thread/([0-9]+)/?$ /links/comments.php?id=$1
RewriteRule ^links/thread/([0-9]+)/([0-9]+)/?$ /links/comments.php?id=$1&page=$2

If you move the code down into the /links directory, then the "links/" in the RewriteRule patterns can/should be removed:

RewriteRule ^thread/([0-9]+)/?$ /links/comments.php?id=$1
RewriteRule ^thread/([0-9]+)/([0-9]+)/?$ /links/comments.php?id=$1&page=$2

Furthermore, it looks like you could improve performance by using the [L] flag to stop the RewriteEngine if a given rule matches. In this case, there is little use in processing the rules that follow it. Using the same example, you'd get:

RewriteRule ^thread/([0-9]+)/?$ /links/comments.php?id=$1 [L]
RewriteRule ^thread/([0-9]+)/([0-9]+)/?$ /links/comments.php?id=$1&page=$2 [L]

Jim

riverstyx

6:51 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



Thanks, but the problem actually seems to be in the script itself (adding a trailing slash to the base directory name). I've posted to their support forum. I appreciate your input.

RS