Mod Rewrite Help

Discussion in 'PHP' started by adamjblakey, Jun 26, 2007.

  1. #1
    Hi,

    I am trying to set-up a mod rewrite on my site.

    Basically i have a urls which come out like:

    recipe-details.php?id=22&title=brie kisses

    This is just an example of one of the urls that i have but what i want it to do is:

    /recipedetails/brie-kisses.html

    Any ideas anyone?

    Cheers,
    Adam
     
    adamjblakey, Jun 26, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    RewriteEngine On
    RewriteRule ^recipedetails/(.*)\.html$ recipe-details.php?title=$1
    
    Code (markup):
    If you want to take out the ID you will have to rewrite your PHP script so that it finds the recipe only by the title.
     
    nico_swd, Jun 26, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thanks for your reply nico, i have added this but does not seem to work for me.

    I have created a .htaccess file and added the code above. I then placed this in the public_html folder but when i go to e.g. foundrecipes.com/recipe-details.php?title=beefy%20cheese%20ball

    It does not rewrite anything. Am i doing something wrong at all?
     
    adamjblakey, Jun 27, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Go to:
    foundrecipes.com/recipedetails/brie-kisses.html

    This should replace the old ugly URL.
     
    nico_swd, Jun 27, 2007 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Ah i think we have crossed wires here.

    What i want it to do is auto fix any url when click for example when i search for beef and then the results show i want want the urls to automatically change when pressed to e.g. recipedetails/brie-kisses.html
     
    adamjblakey, Jun 27, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    You could try placing something like this in recipe-details.php.

    
    
    $page_title = /* adjust for your needs */;
    
    if (preg_match('/\.php$/i', basename($_SERVER['REQUEST_URI'])))
    {
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: recipedetails/' . preg_replace(array('/[^\w-]+/', '/-{2,}/'), '-', $page_title) . '.html');
        exit();
    }
    
    PHP:
     
    nico_swd, Jun 27, 2007 IP
  7. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #7
    I have added the above but did not seem to work :(

    This is what i put, but when i go to the page still comes up with e.g. foundrecipes.com/recipe-details.php?title=rum%20butter%20brie%20with%20fruit

    	$page_title = "recipe-details.php?title=$_GET[title]";
    
    	if (preg_match('/\.php$/i', basename($_SERVER['REQUEST_URI'])))
    	{
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: recipedetails/' . preg_replace(array('/[^\w-]+/', '/-{2,}/'), '-', $page_title) . '.html');
        exit();
    }
    Code (markup):
    Thank you for your help on this nico.
     
    adamjblakey, Jun 27, 2007 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Any ideas on this one?
     
    adamjblakey, Jun 28, 2007 IP