What is wrong with this rewrite code?

Discussion in 'Apache' started by Blinksy, Jul 9, 2007.

  1. #1
    Hi guys,

    Get a 500 whenever I put this in the htaccess file.

    RewriteRule ([0-9]+)/(.*?)/(.*?)/index.php?lyric=$1 
    Code (markup):
    Im foreign when it comes to this. Any ideas?

    The complete htaccess is this

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteRule letters/(.*)/ /index.php?letter=$1
    RewriteRule artists/(.*)/ /index.php?band=$1
    RewriteRule ([0-9]+)/(.*?)/(.*?)/index.php?lyric=$1  
    Code (markup):
    Works whenever I take the last line out.

    Cheers.
     
    Blinksy, Jul 9, 2007 IP
  2. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #2
    Try again with this:

    RewriteRule ^([0-9]+)$ /index.php?lyric=$1
     
    VimF, Jul 9, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    RewriteRule ([0-9]+)/(.*?)/(.*?) /index.php?lyric=$1

    I assume that's what you were aiming for - you need to separate the pattern and destination with a space, otherwise it doesn't make any sense.
     
    rodney88, Jul 10, 2007 IP
  4. Blinksy

    Blinksy Well-Known Member

    Messages:
    415
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Thanks guys.

    With your code VimF, I get a 404 not found.

    With yours Rodney, I still get a 500
     
    Blinksy, Jul 28, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    I have noticed some servers don't like some regex, but have never found any info about this. Try taking parts of the pattern out until you find the issue.

    .*? is the same as .* by the way

    Try this:
    RewriteRule ([0-9]+)/(.+)?/(.+)? /index.php?lyric=$1
    Code (markup):
     
    krt, Jul 28, 2007 IP
  6. Blinksy

    Blinksy Well-Known Member

    Messages:
    415
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #6
    Hey KRT. Seems to have worked, however, all the lyrics now have a

    str_ireplace call to undefined function.


    	global $quarter_banner, $tower, $square;
    	$query  = "SELECT id, title, artist, lyrics, album FROM `lyrics` WHERE id = '" . $id . "'";
    	$result = mysql_query($query);
    	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    	{
    		echo '<div id="' . $row['title'] . '" class="album"><h3 style="margin-left: 0; background-color: #f9f9f9; margin-bottom: 5px; color: #333333;">' . $row['title'] . ' by: ' . $row['artist'] . '</h3>';
    		$high = $_REQUEST['high'];
    		$lyrics = str_ireplace($high, "<span style=\"background-color: #FFFF66\">$high</span>", $row['lyrics']);
    		$arr = array("artist" => $row['artist'], "title" => $row['title'], "album" => $row['album'], "lyrics" => $lyrics, "id" => $row['id']);
    		return $arr;
    	}
    }
    
    Code (markup):
    I am very new to this. So can someone tell me if this is linked to the rewrite code at all?
     
    Blinksy, Jul 29, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
    str_ireplace is only available for PHP5. I take it you have moved the script to a new server that support PHP4, not PHP5?

    Here is a workaround for that issue:
    Change the line:
    $lyrics = str_ireplace($high, "<span style=\"background-color: #FFFF66\">$high</span>", $row['lyrics']);
    PHP:
    To:
    $lyrics = preg_replace('/' . preg_quote($high, '/') . '/i', '<span style="background-color: #FFFF66">$1</span>', $row['lyrics']);
    PHP:
    Come to think of it, the preg_replace() solution is better for this case as it preserves the letter casing.
     
    krt, Jul 29, 2007 IP
    Blinksy likes this.
  8. agnivo007

    agnivo007 Peon

    Messages:
    4,290
    Likes Received:
    289
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The ^ symbol is needed before you begin the actual url.
     
    agnivo007, Jul 29, 2007 IP
  9. Blinksy

    Blinksy Well-Known Member

    Messages:
    415
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #9
    Thank you all for your help. It is all up and running :)
     
    Blinksy, Jul 29, 2007 IP
  10. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #10
    Good observation. It isn't needed though, but it would be better with it.

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteRule ^letters/(.*)/ /index.php?letter=$1
    RewriteRule ^artists/(.*)/ /index.php?band=$1
    RewriteRule ^([0-9]+)/(.+)?/(.+)? /index.php?lyric=$1
    Code (markup):
    The ^ means that the pattern that is being matched has to be at the start (right after the domain name).
     
    krt, Jul 29, 2007 IP