Better Rewrites with php possible?

Discussion in 'Web Design and Development' started by Cabbit, Oct 13, 2009.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    Is it possible to have php set up my
    HTML:
    ## PHP REWRITES ##
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([a-zA-Z0-9_-]+)$ html/index.php?page=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ html/index.php?page=$1
    
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ html/index.php?page=$1&process=$2
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ html/index.php?page=$1&process=$2
    Instead of having to manually input different ones into the .htaccess file i would rather able to sent it one from my php file, perhaps making a function so i could tell it were having the "page/date/process" instead of just the "page/date".
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    The only thing I've seen like that is what WordPress does, but it's hard to follow their code for quick guidance on it. If you have it installed check the file /includes/rewrite.php. The issue is that in order for PHP to handle a case, that PHP has to be accessed, which means the visitor has to start somewhere. I don't think any PHP solution you find will be as good as the Apache one.

    Here's some simplification of your rewrites though,
    Code:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([a-zA-Z0-9_-]+)/?$ html/index.php?page=$1
    
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ html/index.php?page=$1&process=$2
    You could probably even simplify things more using,
    Code:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([^/]+)/?$ html/index.php?page=$1
    
    RewriteRule ^([^/]+)/([^/]+)/?$ html/index.php?page=$1&process=$2
    though you'd want to test that one out first. I'm mostly sure about it though.
     
  3. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #3
    For your case it is appropriate or sensible to redirect all requests to a single php file which then serves up the appropriate content based on url.
     
  4. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #4
    With this in mind how do i tell the server to rewrite

    /html/index.php?page=blog&id=12&date=13112009&title=First_Post&location=readpost

    to

    /blog/13/11/2009/First_Post/readpost

    or for another module

    html/index.php?page=forum&location=showthread&id=13&title=First_Thread&page=3

    to

    /forum/showthread/13/First_Thread/page_3

    or
    html/index.php?location=edit/id=12

    to
    /edit/12
     
  5. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #5
    One thing i have just thought of is the possibility of using an array.

    Code:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([^/]+)/?$ html/index.php?location=$1
    
    PHP:
    $data[0] = $page
    $data
    [1] = $id
    $data
    [2] = $date
    $data
    [3] = $location
    Which i would assume would mean that i could have a "complex" navigation structure with very little code compared to a manual method.

    Any thoughts on this method?
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    I don't currently have the time to work through those rewrites, but here's a decent write up that may help you. You also do searches on "apache rewrite pretty urls" to find other tutorials. I've seen what you want to do done before.
     

Share This Page