Any mod_rewrite demons in da house?

Status
Not open for further replies.

LazyHippy

New member
Apr 9, 2007
3,446
21
0
This is really doing my head in! It was fucking working fine and now it's not... argh!!

It's a lyrics site and I need to do 2 things:

301 artist.lyricssite.com to lyrics.lyricssite.com/lyrics/search.php?find=artist

and also rewrite the other urls to lyrics.lyricssite.com/lyrics/letter/artist/album/title

It was working fine with the lyrics subdomain added in cpanel before I moved servers, now it doesn't like that but it should be doable with just htaccess and the wildcard dns entry.....

I wouldn't mind ditching the lyrics subdomain really, only keeping it as I had other stuff there and it's currently indexed like that.

Here's what I've got at the mo (complete with other stuff I've tried commented out!)

root .htaccess:

Code:
Options +FollowSymlinks
#Options +Indexes
RewriteEngine On
#RewriteBase /

#RewriteCond %{REQUEST_URI} ^lyrics\.lyricssite\.com$
#RewriteRule ^lyrics/([^/]*)\.html$ /index.php?letter=$1 [L]
#RewriteRule ^lyrics/([^/]*)/([^/]*)\.html$ /index.php?letter=$1&artist=$2 [L]
#RewriteRule ^lyrics/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?letter=$1&artist=$2&album=$3 [L]
#RewriteRule ^lyrics/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?letter=$1&artist=$2&album=$3&title=$4 [L]

#RewriteCond %{HTTP_HOST} ^lyrics\.(.*)
#RewriteRule ^(.*)$ http://%1/lyrics/$1 

#RewriteCond %{HTTP_HOST} !lyrics.lyricssite.com$ [NC]
#RewriteCond %{HTTP_HOST} ^(lyrics.)?([a-z0-9-]+).lyricssite.com [NC]
#RewriteRule (.*) %2/$1 [L]

#to prevent rewrite loops
RewriteCond %{REQUEST_URI} !^/index.php?(.*)?$
RewriteCond %{REQUEST_URI} !^/search.php?(.*)?$
# if not lyrics.
#RewriteCond %{HTTP_HOST} !^lyrics\.lyricssite\.com$
# match the subdomain
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.lyricssite\.com$ [NC]
# append the path
RewriteRule ^(.*)$ /lyrics/search.php?find=%2&field=artist [R=301, L]
in lyrics folder:

Code:
RewriteEngine On
RewriteRule ^lyrics/([^/]*)\.html$ /index.php?letter=$1
RewriteRule ^lyrics/([^/]*)/([^/]*)\.html$ /index.php?letter=$1&artist=$2
RewriteRule ^lyrics/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?letter=$1&artist=$2&album=$3
RewriteRule ^lyrics/([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?letter=$1&artist=$2&album=$3&title=$4 [L]

It was sending everything to find=lyrics, now I'm getting a 500 error. Prob something stupid... anyone help? pretty please!!
 


Ok, the server error was something stupid (space between R=301 & L), but now I'm back with the stuff that was working but it's in a fuckin loop... argh!

Anyone good with this shit?
 
I haven't tested this, but I think it will work. Sometimes regex's get so frustrating, it's easier to just start over and try it a different way:

Code:
RewriteEngine On

RewriteRule ^http://lyrics.lyricssite.com/lyrics/(.*?)/(.*?)/(.*?)/(.*?)$ http://lyrics.lyricssite.com/lyrics/index.php?letter=$1&artist=$2&album=$3&title=$4 [L,NC]

RewriteRule ^http://lyrics.lyricssite.com/lyrics/(.*?)/(.*?)/(.*?)/(.*?)/$ http://lyrics.lyricssite.com/lyrics/index.php?letter=$1&artist=$2&album=$3&title=$4 [L,NC]

RewriteRule ^http://lyrics.lyricssite.com/lyrics/(.*?)/(.*?)/(.*?)$ http://lyrics.lyricssite.com/lyrics/index.php?letter=$1&artist=$2&album=$3 [L,NC]

RewriteRule ^http://lyrics.lyricssite.com/lyrics/(.*?)/(.*?)/(.*?)/$ http://lyrics.lyricssite.com/lyrics/index.php?letter=$1&artist=$2&album=$3 [L,NC]

RewriteRule ^http://lyrics.lyricssite.com/lyrics/(.*?)/(.*?)$ http://lyrics.lyricssite.com/lyrics/index.php?letter=$1&artist=$2 [L,NC]

RewriteRule ^http://lyrics.lyricssite.com/lyrics/(.*?)/(.*?)$ http://lyrics.lyricssite.com/lyrics/index.php?letter=$1&artist=$2 [L,NC]

RewriteRule ^http://lyrics.lyricssite.com/lyrics/(.*?)$ http://lyrics.lyricssite.com/lyrics/index.php?letter=$1 [L,NC]

RewriteRule ^http://lyrics.lyricssite.com/lyrics/(.*?)/$ http://lyrics.lyricssite.com/lyrics/index.php?letter=$1 [L,NC]

RewriteRule http://(.*?).lyricssite.com(.*) http://lyrics.lyricssite.com/lyrics/search.php?find=$1 [L,NC]
 
Thanks, currently I'm using flyernati's code (which isn't working - cheers though). Fuck it, can't see any point hiding the domain really...



With no .htaccess in the lyrics folder at present.

This is so fuckin annoying. I'm in the process of re-designed it and was going to redirect it all to a new site soon, so really hate spending time on this but got 60k pages indexed and don't want that to start dropping.

Cheers for your help guys.

His won't work because he doesn't have a clue and shouldn't be posting code when he doesn't know what the hell he's doing.


Here's your root .htaccess to 301 redirect to lyrics.lyricssite.com

Code:
RewriteEngine on

RewriteCond %{HTTP_HOST} !^lyrics\. [NC]
RewriteCond %{HTTP_HOST} !^www\.lyricssite\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.lyricssite\.com [NC]
RewriteRule .* http://lyrics.lyricssite.com/lyrics/search.php?find=%2&field=artist [R=301,L]
Here's the .htaccess for the lyrics folder. This assumes that index.php is in the root folder, not the lyrics folder

Code:
RewriteEngine on
RewriteBase /

RewriteRule ^([^/]+)\.html$ /index.php?letter=$1 [NC,L]
RewriteRule ^([^/]+)/([^/]+)\.html$ /index.php?letter=$1&artist=$2 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)\.html$ /index.php?letter=$1&artist=$2&album=$3 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)\.html$ /index.php?letter=$1&artist=$2&album=$3&title=$4 [NC,L]
Try that and let me know if that fixes your problem
 
Cheers, I'll give that a shot!

index.php is in the lyrics folder, I don't need to do anything more than prefix it with lyrics/ do I?
 
Cheers, I'll give that a shot!

index.php is in the lyrics folder, I don't need to do anything more than prefix it with lyrics/ do I?

Here's the .htaccess file for the lyrics folder if index.php is in that folder, i.e.

/lyrics/.htaccess
/lyrics/index.php

Code:
RewriteEngine on

RewriteRule ^([^/]+)\.html$ index.php?letter=$1 [NC,L]
RewriteRule ^([^/]+)/([^/]+)\.html$ index.php?letter=$1&artist=$2 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)\.html$ index.php?letter=$1&artist=$2&album=$3 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?letter=$1&artist=$2&album=$3&title=$4 [NC,L]
 
Cheers.

The rewrite part works fine (broke the css but fine after changing the ref to the stylesheet to absolute), but the subdomain 301 is showing open directories.

I also need to send site.com & lyrics.site.com to site.com/lyrics/ (sorry, wasn't in code above as), would that be best before or after the 301 to search.php?

Thanks for ya help with this man.
 
Ignore the open directory bit... it's just going to the root, no index there as it redirected before. Very tempted to do it all with php but really wanna get my head around mod_rewrite!
 
I also need to send site.com & lyrics.site.com to site.com/lyrics/ (sorry, wasn't in code above as), would that be best before or after the 301 to search.php?

OK, I think this is what you're looking for


root .htaccess

Code:
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(lyrics\.|www\.)?lyricssite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/lyrics/ [NC]
RewriteRule (.*) lyrics/$1 [L]

RewriteCond %{HTTP_HOST} !^lyrics\. [NC]
RewriteCond %{HTTP_HOST} !^www\.lyricssite\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.lyricssite\.com$ [NC]
RewriteRule .* http://lyrics.lyricssite.com/lyrics/search.php?find=%2&field=artist [R=301,L]

If you want to redirect to the lyrics folder you would change

RewriteRule (.*) /lyrics/$1 [L]

to

RewriteRule (.*) /lyrics/$1 [R=301,L]

-

Your lyrics folder .htaccess

Code:
DirectoryIndex index.php

RewriteEngine on

RewriteRule ^([^/]+)\.html$ index.php?letter=$1 [NC,L]
RewriteRule ^([^/]+)/([^/]+)\.html$ index.php?letter=$1&artist=$2 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)\.html$ index.php?letter=$1&artist=$2&album=$3 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)\.html$ index.php?letter=$1&artist=$2&album=$3&title=$4 [NC,L]
 
  • Like
Reactions: LazyHippy
Status
Not open for further replies.