Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I need a little help with .htaccess file I have songs, singers and albums links I want to rewrite.

I all ready rewrote the links and they are like this: the links for the songs is like this:

/song/song_name

for singers:

/singer_name

for albums:

/album_name

From my .htaccess file:

RewriteEngine on
RewriteRule ^singer/([^/\.]+)/?$ /core/controller.php?singer=$1 [L]
RewriteRule ^song/([^/\.]+)/?$ /core/controller.php?song=$1 [L]
RewriteRule ^album/([^/\.]+)/?$ /core/controller.php?album=$1 [L]

I need the links for the songs, singers and albums to be like this:

for songs /singer_name/song_name

for singers /singer_name

for albums /singer_name/album_name

can anyone help me with this please.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You cannot do this in .htaccess unless you pre-define (hard code) all names in advance (well, you can use RewruteMap but that is close to hard-coding). The reason is -- how can you identify what is song_name and what is album_name if they both have the same syntax? Also .. sometimes you have situations when album name is a song name at the same time (album named after the song) -- how can you identify which one is which?

You have few options here:

1) Redirect everything to your controller.php and implement routing logic inside (using PHP only). There you can query database and see what the actual parameter is -- album or song.

2) Implement 3-tier URL structure (this will allow RewriteRules to properly identify what is what):

/singer-name for singers

/singer-name/album-name for albums

/singer-name/album-name/song-name for songs

3) Add some additional info/code into URL so mod_rewrite can see the difference between album-name & song_name. For example:

/singer-name for singers

/singer-name/album-name_a for albums (_a will tell that this is the album)

/singer-name/song-name_s for songs (_s will tell that this is the song)

or

/singer-name/s_song-name for songs

or add no such code for songs, as albums URLs will have such code already.


I personally prefer #2 -- it has more logical hierarchy.


UPDATE: These are the rules for scenario #2. You can use these characters: any latin letter (upper and lower case), digits, underscore _ and minus -. This will cover the following URLs:

/britney-spears -- singer url

/britney-spears/Femme-Fatale -- album url

/britney-spears/Femme-Fatale/Till-the-World-Ends -- song url

RewriteEngine On
# Do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# work with artist URLs
RewriteRule ^([a-z0-9\-_]+)$ /core/controller.php?singer=$1 [NS,QSA,L]
# work with album URLs
RewriteRule ^([a-z0-9\-_]+)/([a-z0-9\-_]+)$ /core/controller.php?singer=$1&album=$2 [NS,QSA,L]
# work with songs URLs
RewriteRule ^([a-z0-9\-_]+)/([a-z0-9\-_]+)/([a-z0-9\-_]+)$ /core/controller.php?singer=$1&album=$2&song=$3 [NS,QSA,L]

It's up to you if you going to use all lower case letters or mixed-case. I would recommend all lower case for consistency.

share|improve this answer
    
yeah i prefer the #2 as well but how can i actually do it because i'm new to regular expressions... btw.. i'm identifying them through a singer_url or song_url or album_url columns in the database. –  Pinokyo Jun 19 '11 at 12:16
    
Database column names are irrelevant here. If you are talking about the actual URLs (album_name vs album-name) then the second one is recommended (use - instead of _ ). I will update my answer with exact rules for #2 scenario. –  LazyOne Jun 19 '11 at 12:20
    
thank u very much –  Pinokyo Jun 19 '11 at 13:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.