views:

42

answers:

3

Hi guys

I have two URLs that I would like to rewrite:

  1. /artist.php?name=x => /x

  2. /albums.php?title=y => /y

This is what I put in the .htaccess file:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ artist.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ artist.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ albums.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ albums.php?name=$1

How should I edit the file so that the albums.php's URL would be executed as well? (Only the first one is working.) Thanks for your help.

+3  A: 

You have duplicate patterns for both artist and album, so they can't possibly both work! Not with just mod_rewrite anyway. (Your web app might be able to check for the existence of an artist or album with the supplied name parameter, and decide which one is intended, but that's beyond the scope of a simple mod_rewrite example.)

There needs to be something else in the URL to distinguish between artist and album, for example:

RewriteRule ^/artist/([a-zA-Z0-9_-]+)/?$ artist.php?name=$1
RewriteRule ^/album/([a-zA-Z0-9_-]+)/?$ albums.php?name=$1

Notice also that I shortened your 4 rewrite rules to 2, using /? (? is a quantifier meaning "zero or one") to allow the URL with or without a trailing slash. But in my opinion it would be altogether better to choose one or the other, to avoid having 2 URLs for the same page.

Ben James
A: 

Right now I have:

RewriteRule ^([A-Za-z0-9-]+)/?$ profile.php?user=$1

But I want two variables so I will have user=$1&s=$2

How do I do that so that it would look like this:

http://.../tester/1

Emily
A: 
RewriteRule ^/artist/([a-zA-Z0-9_-]+)/?$ artist.php?name=$1 [L]
RewriteRule ^/album/([a-zA-Z0-9_-]+)/?$ albums.php?name=$1 [L]
Jordan