SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: php mod rewrite

  1. #1
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php mod rewrite

    I am currently stuck trying to work out a mod rewrite issue that I have been stuck on for a while.

    What I am trying to do is convert every url within my website so that it looks like this:



    So every page within the website will always have its url broken down and separated by a /.

    Thanks, J

  2. #2
    SitePoint Member
    Join Date
    Oct 2006
    Location
    Pretoria, South Africa
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use preg_replace

  3. #3
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg_replace will do a find and replace on a string... but it wont tell apache to look for a different url.

    I am pretty sure a mod_rewrite is needed in the .htaccess file.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    The rewrite code: (Not tested!)
    Code:
    RewriteCond %{REQUEST_URI} ^/?(index|otherpage) [NC]
    RewriteRule .? %1.php?d=%{REQUEST_URI} [QSA,L]
    PHP Code:
    <?php

    if ( isset( $_GET['d'] ) ) {

        
    $parts explode'/'$_GET['d'] );
        
    array_shift$parts ); // Remove the file entry.

        
    for ( $i 0$l count$parts); $i $l$i++ ) {
            
    $_GET$parts$i ] ] = $parts[ ++$i ];
        }

    }

    // Go one like mod rewrite was never used.
    Write your code like you are not using mod rewrite.
    Last edited by logic_earth; Apr 14, 2008 at 01:02.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much for your help Login_Earth!

    So with your code I should be able to use:

    www.testurl.com/page1.php?id=1&ie=f --> www.testurl.com/page1/id/1/ie/f

    and also if needed

    www.testurl.com/page2.php --> www.texturl.com/page2

    I take it with the php code it will break the variable into an array. So I could then simply use $id and $ie for the above example?

    Does anyone know if this will harm my seo results without having a file extension?

    Thanks, J

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Yes it should handle that, some testing may be required.

    I have it setup to populate $_GET so it will be $_GET['id'] and $_GET['ie']
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats great Logic_Earth

    I have created the .htaccess file with your code.. but when I try to go to www.texturl.com/page2 it cannot find the correct (www.testurl.com/page2.php) page.

    J

  8. #8
    SitePoint Enthusiast jameso's Avatar
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I vaguely remember a PHP or Apache setting that would allow URLs in the form of /index.php/id/5 without having to do any fancy coding yourself.

    Is anyone aware of what this setting is, or am I just making stuff up?

    James

  9. #9
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jameso View Post
    I vaguely remember a PHP or Apache setting that would allow URLs in the form of /index.php/id/5 without having to do any fancy coding yourself.

    Is anyone aware of what this setting is, or am I just making stuff up?

    James
    This does it for me:
    Code:
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^(.*)$ index.php
    PHP Code:
    $requestURI $_SERVER['REQUEST_URI'];
    $requestURI ltrim($requestURI'/');
    $requestURI rtrim($requestURI'/');
        
    $urlPieces explode('/'$requestURI);
        
    $controller = (array_key_exists(0$urlPieces) && !empty($urlPieces[0])) ?
                      
    ucfirst($urlPieces[0]) : 'Pages';

    $resource = (array_key_exists(1$urlPieces) && !empty($urlPieces[1])) ?
                     
    $urlPieces[1] : 'home';
        
    $action = (array_key_exists(2$urlPieces) && !empty($urlPieces[2])) ?
                  
    $urlPieces[2] : 'read';

    $format = (array_key_exists(3$urlPieces) && !empty($urlPieces[3])) ?
                 
    $urlPieces[3] : 'HTML'
    I will not flame the newbies,
    I will not flame the newbies,
    I will flame the newbies...
    Table free is the way to be!

  10. #10
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks BluDragon

    So does this script simply modify the url for the index page only. Then it will create an array with the values called pages, home, read and html?

    J

  11. #11
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use that for any page (I wrap it into a function). How it works is if the page is served with Apache then all requests that are not a valid file or directory are routed to index.php. From there, it takes everything after the first / and maps it to variables that can be used for routing the request. I wrote it so that it also works without Apache.

    An example of how it works:

    With Apache:
    "http://somesite/pages/home/read" goes to "http://somesite/index.php". "pages" is then mapped to "$controller", "home" to "$resource", and so on.

    Without Apache it does the same but with one minor difference:
    "http://somesite/index.php/pages/home/read/"

    Hope that helps! Glad you appreciate it.
    I will not flame the newbies,
    I will not flame the newbies,
    I will flame the newbies...
    Table free is the way to be!

  12. #12
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just noticed a nasty bug in the code I posted above. Turns out I never really tested it out with using "index.php/var1/var2" -- just "index/var/var2". Tonight I was refactoring that piece of code to load paths from an XML file for better extensibility. I copied that code to "xml.php" and proceeded to call "xml.php/var" and noticed something strange in the print out of "$uriPieces": "xml.php" was actually set at key zero! I whipped up a q&d piece of code to fix it but there may be other/better ways.

    PHP Code:
    if (array_key_exists(0$uriPieces) && (substr($_SERVER['SCRIPT_NAME'], 1) == $uriPieces[0]))
    {
      
    array_shift($uriPieces);

    Enjoy! Any questions/comments please feel free to mention them.
    I will not flame the newbies,
    I will not flame the newbies,
    I will flame the newbies...
    Table free is the way to be!

  13. #13
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $uri explodebasename__FILE__ ), $_SERVER['REQUEST_URI'] );
    $uri explode'/'trim$uri[1], '/ ' ) );
    $uri array_pad$uri4null );

    $controller = empty( $uri[0] ) ? 'Default' $uri[0];
    $resource   = empty( $uri[1] ) ? 'Default' $uri[1];
    $action     = empty( $uri[2] ) ? 'Default' $uri[2];
    $format     = empty( $uri[3] ) ? 'Default' $uri[3];
    $leftovers  array_splice$uri); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •