Forum Moderators: phranque

Message Too Old, No Replies

Problems with mod rewrite and a trailing slash

         

Melfra

5:13 pm on Apr 23, 2010 (gmt 0)

10+ Year Member



I've been working for a while on this htaccess file, but I'm still new to this stuff, so hopefully someone can see where I've gone wrong.

I've made a reader for some translations with books split into series, then chapter, then page, and set up so if the user wants, they can just type in numbers manually. The htaccess file (as well as the reader.php file that this is based off) is in the reader directory, and the url structure is something like reader/series/chapter/page/. The basic idea is, if someone specifies just the chapter, it will go to the first page, and if they specify the series, then it goes to the series page.

It works fine as long as the url typed in is reader/series/, but I can't figure out how to get the page to redirect properly if the trailing slash is omitted without borking up pretty much everything else. Most of the book names have underscores in them, otherwise (.+) could be switched for ([a-z]) for the series (as far as I can tell at least), but I haven't been able to try that.

RewriteEngine on

RewriteRule ^(.+)/([0-9]+)/([0-9]+)$ /reader/$1/$2/$3/ [NC,R,L]
RewriteRule ^(.+)/([0-9]+)/([0-9]+)/$ /reader/reader.php?series=$1&ch=$2&page=$3 [NC,L]

RewriteRule ^(.+)/([0-9]+)/$ /reader/$1/$2/1/ [NC,R,L]
RewriteRule ^(.+)/([0-9]+)$ /reader/$1/$2/1/ [NC,R,L]

RewriteRule ^(.+)/$ /reader/series.php


I'm not sure if I'm doing things wrong, but at first I thought specifying RewriteBase to the directory above and adding reader/ onto all the urls would work- no luck there... Well, like I said, I'm really new to this, so I'd really be happy for any advice!

jdMorgan

7:40 pm on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Include the underscore in the alternate-character groups, then. With additional changes:

# Externally redirect to add missing trailing slash
RewriteRule ^([a-z_]+/([0-9]+/)?([0-9]+)?)$ http://www.example.com/$1/ [NC,R=301,L]
#
# Internally rewrite to reader.php or series.php script
RewriteRule ^([a-z_]+)/([0-9]+)/([0-9]+)/$ /reader/reader.php?series=$1&ch=$2&page=$3 [NC,L]
RewriteRule ^([a-z_]+)/([0-9]+)/$ /reader/reader.php?series=$1&ch=$2&page=1 [NC,L]
RewriteRule ^([a-z_]+)/$ /reader/series.php [NC,L]

Note that to avoid duplicate content, your scripts should enforce a standard casing on the requested URLs, and generate a 301 redirect response to the properly-cased URL if an improperly-cased URL is requested.

If you do this, then you'll want to remove the first (redirect) rule above and make the trailing slashes optional in the three rewrite rules (change "/$" at the end to "/?$"). Then let the script handle both case-correction and missing-trailing-slash redirects all at once. This will avoid having chained/stacked multiple redirects, which are not good for either search engines or for users.

Jim

Melfra

1:04 am on Apr 24, 2010 (gmt 0)

10+ Year Member



Thanks so much for your help! I had no idea alternate characters could be specified right in there. I mean, I'd hoped, but my google-fu must be lacking lately. Your suggestion worked perfectly, too. Man, am I a happy camper!

By the way, if you don't mind me asking, how would I go about filling out the missing parts of the url (like the page number and slash) at once with the rewrite you have up there? The external redirect only worked when the trailing slash after the page number was omitted- if I cut off after the chapter number I get a browser error saying the request couldn't be completed, and if I cut off at the end of the series name, it wouldn't add a trailing slash. Not that the latter is much of an issue, but the former might be a problem. Thank you again for being so helpful!

jdMorgan

4:49 pm on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, I wrote that too fast. Probably better to be explicit about the missing-trailing-slash URL-path formats:

# Externally redirect to add missing trailing slash
RewriteRule ^([a-z_]+/([0-9]+/)*[0-9]+)$ http://www.example.com/$1/ [NC,R=301,L]
RewriteRule ^([a-z_]+)$ http://www.example.com/$1/ [NC,R=301,L]
#
# Internally rewrite to reader.php or series.php script
RewriteRule ^([a-z_]+)/([0-9]+)/([0-9]+)/$ /reader/reader.php?series=$1&ch=$2&page=$3 [NC,L]
RewriteRule ^([a-z_]+)/([0-9]+)/$ /reader/reader.php?series=$1&ch=$2&page=1 [NC,L]
RewriteRule ^([a-z_]+)/$ /reader/series.php [NC,L]

The resources cited in our Apache Forum Charter (see the link at the top of this screen) will likely be very useful, and I commend to you.

Jim