views:

110

answers:

2

Hello,

Considering the following rules, how can I rewrite them in order have a trailing slash? I have to mention that I can only edit the .htaccess file, so I have no access to modify the URLs in the website.

RewriteRule ^artists/(.*)-p(.*)$ /artists.php?l=$1&p=$2 [QSA,L]
RewriteRule ^artists/(.*)$ /artists.php?l=$1 [QSA,L]
RewriteRule ^lyrics/(.*)/(.*)$ /artists-albums.php?a=$1&b=$2 [QSA,L]
RewriteRule ^lyrics/(.*)$ /artists-details.php?a=$1 [QSA,L]
RewriteRule ^p-(.*)-(.*)$ /index.php?p=$1&q=$2 [QSA,L]

So, if someone is looking up /lyrics/abba it has to automatically redirect to /lyrics/abba/.

A: 

Add another rule to rewrite things that don't end in a slash, before the other rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ $1/ [L,R=301]

(I may have borked the exact regex on that one, but you get the idea.)

Amber
A: 

Try this rule in front of your other rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $0 !.*\.php$
RewriteRule .*[^/]$ /$0/ [L,R=301]

But you should also modify your existing rules by replacing .* with at least .+ or better [^/]+:

RewriteRule ^artists/([^/-]+)-p([^/]+)$ /artists.php?l=$1&p=$2 [QSA,L]
RewriteRule ^artists/([^/]+)$ /artists.php?l=$1 [QSA,L]
RewriteRule ^lyrics/([^/]+)/([^/]+)$ /artists-albums.php?a=$1&b=$2 [QSA,L]
RewriteRule ^lyrics/([^/]+)$ /artists-details.php?a=$1 [QSA,L]
RewriteRule ^p-([^/-]+)-([^/]+)$ /index.php?p=$1&q=$2 [QSA,L]

If possible you should use even more specific pattern ([0-9]+ for numbers, [1-9][0-9]* for numbers without 0, etc.).

Gumbo
Psyche
Then just put the rewrite rule for the trailing slash *after* the rules you don't want affected, and *before* the rules you do.
Amber
@Gumbo, I made all the changes above, but now it redirects to some 404.shtml saying "The page isn't redirecting properly".
Psyche