Need some help with mod_rewrite

Discussion in 'PHP' started by belgin fish, Jan 9, 2010.

  1. #1
    Hey, well on my website I'm currently using this url structure to display the movies.

    /video.php?id=(videoid)

    I'm looking to change this using mod_rewrite so that it displays as

    /(movie name).html

    I have no clue how to do this.

    Could anyone help me out? :D
     
    belgin fish, Jan 9, 2010 IP
  2. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #2
    you will need to update your video.php file to accept movie names instead of IDs, and then you just need to use mod_rewrite:

    
    RewriteEngine On
    RewriteCond {REQUEST_URI} !index.html$ [NC]
    RewriteCond {REQUEST_URI} \.html$ [NC]
    RewriteRule (.*).html /video.php?name=$1 [L,R=302]
    
    Code (markup):
    this is from the top of my head, so it may need a little tweaking..
    the first condition excludes the index.html file (not case sensitive),
    the second condition targets all the urls ending with .html
    and the rule is to take the filename without the html and do a 302 redirect (can also be 301) to the correct form you'll wanna use.
     
    yoavmatchulsky, Jan 9, 2010 IP
  3. belgin fish

    belgin fish Well-Known Member

    Messages:
    1,544
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    185
    Digital Goods:
    2
    #3
    thanks :D Exactly what I was looking for. Do I have to remove the id's ? Or could i just add in the title?
     
    belgin fish, Jan 9, 2010 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well you could use MovieName-id.html like Finding-nemo-54.html where 54 is the movie id and then use
    RewriteRule -(\d+).html$ video.php?id=$1 [L]
    Code (markup):
    That should do it
     
    JAY6390, Jan 9, 2010 IP
  5. belgin fish

    belgin fish Well-Known Member

    Messages:
    1,544
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    185
    Digital Goods:
    2
    #5
    ok i made it so the url is /video/id/moviename.html
     
    belgin fish, Jan 9, 2010 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    in that case you just need
    RewriteEngine On
    RewriteBase /
    RewriteRule ^video/(\d+)/(.*?)\.html$ /video.php?id=$1 [L]
    Code (markup):
    I am assuming that your id is a number and not letters as well
     
    JAY6390, Jan 9, 2010 IP