views:

68

answers:

3

Hello,

I'm having some issues with a mod_rewrite rule. I have to make 2 types of links (the first one is for the artists that have an album, the second one is the for the ones that don't): 1) /lyrics/artist-name/album-name/song-name.php 2) /lyrics/artist-name/song-name.php

My code looks like this:

RewriteRule ^lyrics/(.*)/(.*)/(.*).php$ /artists-lyrics.php?a=$1&b=$2&c=$3 [QSA,L]
RewriteRule ^lyrics/(.*)/(.*).php$ /artists-lyrics.php?a=$1&b=$2 [QSA,L]

Sadly, only the first rule works. How can I change them in order to make them both work?

Thank you.

A: 

The second rule never works, because the first rule matches "/lyrics/artist-name/song-name.php" aswell: (.*) means: any number of characters (even zero).

Without having the possibility to test it, this may work:

RewriteRule ^lyrics/([^\]+)/([^\]+).php$ /artists-lyrics.php?a=$1&b=$2 [QSA,L]
RewriteRule ^lyrics/([^\]+)/([^\]+)/([^\]+).php$ /artists-lyrics.php?a=$1&b=$2&c=$3 [QSA,L]
FlorianH
I get a 500 Server Error with those rules :(
Psyche
+1  A: 

You need to make your pattern more specific. Try using [^/]+ (one or more characters except /) instead of .* (any characters of any length):

RewriteRule ^lyrics/([^/]+)/([^/]+)/([^/]+)\.php$ /artists-lyrics.php?a=$1&b=$2&c=$3 [QSA,L]
RewriteRule ^lyrics/([^/]+)/([^/]+)\.php$ /artists-lyrics.php?a=$1&b=$2 [QSA,L]
Gumbo
Psyche
@Psyche: So you didn’t try my proposed rules?
Gumbo
I tried them, but as I said, they don't seem to work.I think it's a conflict with the rule I pasted above.
Psyche
@Psyche: You shouldn’t use my proposed rules additionally to yours but as a substitution for yours.
Gumbo
@psyche: please add the other rule to the question as well - and make sure they are in the order they appear.
gnarf
A: 

Generally you want to set your atom as restrictive as possible to avoid unintended consequences. In the example below, only alphanumeric characters are accepted (which is probably what you want). The + sign denotes one or more, whereas * denotes zero or more.

RewriteRule ^lyrics/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+).php$ /artists-lyrics.php?a=$1&b=$2&c=$3 [QSA,L]
RewriteRule ^lyrics/([a-z0-9]+)/([a-z0-9]+).php$ /artists-lyrics.php?a=$1&b=$2 [QSA,L]
jusunlee