Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    making clean urls using PHP ??

    Hello forums !!
    I am bored with working with the url with query string urls ie("http://localhost/my_project/index.php?page=test&action=create").
    Now i would like to work with clean urls like "http://localhost/my_project/test/create"
    I would like to rewrite the url without using mod_rewrite module rather i would like to use PHP.
    How to explode the urls to get the required parameters ?
    Note: I am working under the my_project folder
    Any comments n suggestions are warmly welcome.
    Thanks in advance to all of you.

  • #2
    New Coder
    Join Date
    Sep 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I always use mod_Rewrite and

    I always use mod_rewrite and rewrite the url but I edit the script so it outputs the correct urls also

  • #3
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok then plz let me know.
    i had the following case:
    if i had the following url: http://localhost/my_project/test/create
    I would to map this url with http://localhost/my_project/index.php?url=/test/create/
    So that its easier to retrieve the params using $_GET['url'] and everything will go fine.
    How it could be achieved using mod_rewrite? thanks in advance.

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts

    mod_rewrite

    to use mod_rewrite, you have to either create or modify the .htaccess file on your server.

    the mod is usually switched off by default, so start with:
    Code:
    RewriteEngine on
    Then tack on your rewrite rules afterwards. This might not be exactly right, but you're looking for something like this:
    Code:
    Rewrite Rule ^/my_project/(.*)$ /index.php?url=$1
    Then when you visit /my_project/test/create, it will reroute you to /my_project/index.php?url=test/create and $_GET['url'] will be "test/create".

    mod_rewrite uses regular expressions, so you might want to familiarize yourself with those as well. Check out some online tutorials:
    http://www.sitepoint.com/article/guide-url-rewriting

    http://www.phpfreaks.com/tutorials/23/0.php

    Hope that helps.

  • #5
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    how to enable $_SERVER['PATH_INFO'], it says undefined index PATH_INFO.

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts
    I'm can't really be sure what the problem is without more info. If you let me know why you're using path info, maybe I can help you figure out a work around.

    I'm guessing that you're trying to use PATH_INFO to figure out what URL the user typed in, but if you use mod_rewrite to rewrite the URL into a $_GET string, you don't have to.

    The user types:
    http:/localhost/my_project/test/create
    mod_rewrite rewrites URL as:
    http://localhost/my_project/index.php?url=test/create

    If you add this to index.php
    Code:
    $url = $_GET["url"];
    Then you know that the full URL is:
    Code:
    $wholeURL = "http://localhost/my_project/".$url
    If you really wanted to figure it out using $_SERVER strings, you could try:
    Code:
    $theUserTyped = 'http://'. $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];
    Let me know if that all works out.
    Last edited by madmatter23; 09-04-2007 at 10:40 PM.

  • #7
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by madmatter23 View Post
    to use mod_rewrite, you have to either create or modify the .htaccess file on your server.

    the mod is usually switched off by default, so start with:
    Code:
    RewriteEngine on
    Then tack on your rewrite rules afterwards. This might not be exactly right, but you're looking for something like this:
    Code:
    Rewrite Rule ^/my_project/(.*)$ /index.php?url=$1
    Then when you visit /my_project/test/create, it will reroute you to /my_project/index.php?url=test/create and $_GET['url'] will be "test/create".

    mod_rewrite uses regular expressions, so you might want to familiarize yourself with those as well. Check out some online tutorials:
    http://www.sitepoint.com/article/guide-url-rewriting

    http://www.phpfreaks.com/tutorials/23/0.php

    Hope that helps.
    your above method didnt worked..
    i tried to print print_r($_GET) nothing appeared..
    Help again on url rewrite

  • #8
    New Coder
    Join Date
    Sep 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey I wouldn't try that maybe give people access to more if

    file=../
    or
    file=../etc/psswd
    be careful stuff like this can happen

  • #9
    New Coder
    Join Date
    Sep 2007
    Posts
    41
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Hey,
    There is an excellent forum called Mod_Rewrite Forum.
    although it's a forum, I never had the need to post a question there, almost anything you could possible ask is already asked and answered there.

    If you are a newcomer, I suggest you first check "The basics of Mod_Rewrite" forums first, and then start browsing around.

    http://forum.modrewrite.com/

    good luck with your project.

  • #10
    Regular Coder
    Join Date
    Sep 2006
    Location
    Vermont, USA
    Posts
    154
    Thanks
    0
    Thanked 6 Times in 6 Posts
    .htaccess
    RewriteEngine On
    RewriteRule ^projects/(.*)/(.*)$ /projects.php?file=$1&action=$2 [QSA,NC,L]

    PHP Code:
    $file = !isset($_GET['file']) || !file_exists('content/'.$_GET['file']) ? 'home.php' $_GET['file'].'.php';

    include 
    'content/'.$file
    Active PHP/MySQL application developer available for immediate work.
    syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready


  •  

    Posting Permissions

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