Mobile app version of vmapp.org
Login or Join
Deb1703797

: I need a little help with .htaccess rewrite 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

@Deb1703797

Posted in: #Htaccess #UrlRewriting

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= [L]
RewriteRule ^song/([^/.]+)/?$ /core/controller.php?song= [L]
RewriteRule ^album/([^/.]+)/?$ /core/controller.php?album= [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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Deb1703797

1 Comments

Sorted by latest first Latest Oldest Best

 

@Shelton105

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= [NS,QSA,L]
# work with album URLs
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)$ /core/controller.php?singer=&album= [NS,QSA,L]
# work with songs URLs
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)$ /core/controller.php?singer=&album=&song= [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme