Clean Url in PHP

Discussion in 'PHP' started by . Jordan ., May 27, 2010.

  1. #1
    I've been wondering how to do this for a while now. Basically, what I want to do is be able to use $_GET variables in the url without the messy symbols and stuff. Here's an example of what I mean.

    Instead of having to use:

    example.com/file.php?var=123

    I want users to be able to use:

    example.com/123

    If anybody could give me a solution for this, I would really appreciate it.



    Thanks,

    . Jordan .
     
    . Jordan ., May 27, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Edit your .htaccess file (usually located in root path, if does'nt exist simply create a file named .htaccess), and place the following contents:

    RewriteEngine on
    RewriteRule ^(.*)$ file.php?var=$1 [L]
    Code (markup):
     
    danx10, May 27, 2010 IP
  3. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Is there no ways to do it within the script itself. Just that if I were to distibute the script, it would be a downside that customers have to edit that file every time.
     
    . Jordan ., May 27, 2010 IP
  4. Travis

    Travis Peon

    Messages:
    539
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well that .htaccess file will automatically do each url (using regex) so long as the structure is the same.

    Unless you plan on changing the structure every 5 minutes, it shouldn't be necessary, just include all the rules that the person running the script would need, and it's good to go.
     
    Travis, May 27, 2010 IP
  5. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The thing is, if I distribute the script, some people who use it may not have the knowledge of editing .htaccess files. I'm looking to make it so that the user can easily just put the script on their site and that's it. Plus, there is only one file which will be using this so its no hassle to be doing it in php.
     
    . Jordan ., May 27, 2010 IP
  6. kaleshwar

    kaleshwar Peon

    Messages:
    43
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    just include the .htaccess file with your script when distributing. It cant be done in php because t needs to be done before the request gets to php.
     
    kaleshwar, May 27, 2010 IP
  7. . Jordan .

    . Jordan . Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The thing is, I know it can be. I've seen it done before and just cant remember the script. Gonna have a play about and see if I get anything.
     
    . Jordan ., May 27, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    Maybe - http://phprewrite.randombase.com/ (however I don't have any experience with it and don't suggest this method)
     
    danx10, May 27, 2010 IP
  9. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #9
    The only other way is to pass all your 404 requests to the php file then parse its URI.

    Google this one. I have done it once or twice before.
     
    NatalicWolf, May 27, 2010 IP
  10. Qc4

    Qc4 Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Most servers will allow you to use URLs like this:

    example.com/file.php/123

    You can then put code inside file.php that checks $_SERVER['PATH_INFO'] (or $_SERVER['REQUEST_URI'] if PATH_INFO isn't supported on your server) and obtains the query string from there.
     
    Qc4, May 28, 2010 IP
    kaleshwar likes this.
  11. hollowmatt

    hollowmatt Greenhorn

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #11
    hey try this one
    I take the function from drupal, hope it can help you

    
    function arg($index = NULL, $path = NULL) {
      static $arguments;
    
      if (!isset($path)) {
        $path = $_GET['q'];
      }
      if (!isset($arguments[$path])) {
        $arguments[$path] = explode('/', $path);
      }
      if (!isset($index)) {
        return $arguments[$path];
      }
      if (isset($arguments[$path][$index])) {
        return $arguments[$path][$index];
      }
    }  
    
    PHP:

    and here is the .htaccess code
    
    <IfModule mod_rewrite.c>
      RewriteEngine on
      # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !=/favicon.ico
      RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    </IfModule>  
    
    Code (markup):

    enjoy it :cool:
     
    hollowmatt, May 28, 2010 IP