#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    US
    Posts
    261
    Rep Power
    15

    PATH_INFO URL - Search Engine Friendly


    Hi:

    I read that to turn URLS like:

    /articles.php?id=51

    You can use $PATH_INFO so you can make it like:

    /articles/51

    Then you just explode the URL using /, and you get your id. I was wondering, well first, we didn't get the $PATH_INFO function, so I used $REQUEST_URI instead. But when I did:

    /articles/51

    It said "Page Not Found" as if it were searching under the directory of "articles" and "51". Could someone please tell me how to get it to work. I know how to get the URL and ID, but I need help in getting it where you can have:

    /articles/51

    Thanks a lot for your help, I really appreciate it!!

    Ken Cooper
    Thanks!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    England
    Posts
    21
    Rep Power
    0
    i have not done ti myself, yet. but plan to in the next few days... but i plan on following this tutorial, using mod_rewrite.

    http://alistapart.zeldman.com/stories/urls/index.html

    also check:

    http://alistapart.zeldman.com/stories/urls/index.html

    and

    http://www.faqts.com/knowledge_base/view.phtml/aid/124
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    226
    Rep Power
    15
    this will help(from a book)

    Manual URL Rewriting
    You can also use manual URL rewriting for session ID propagation.This means that you pass the session ID via GET/POST or you “hide” it in the URL.You need to alter all
    frame, form, and a HTML tags to include a reference to your ID:

    printf(‘<frame src=”page.php3?session_id=%s”>’, $session_id);

    printf(‘<input type=”hidden” name=”session_id” value=”%s”>’, $session_id);

    printf(‘<a href=”page.php3?session_id=%s”>Link</a>’, $session_id);
    If you have image maps, inline frames, or JavaScript redirects in your application, you’ll also need to alter those.
    URL rewriting has several drawbacks:
    n
    It introduces a considerable amount of additional work for you as developer.You have to manually add the session ID to all links. If you forget a single link, the user’s session will be lost.


    HTTP and Sessions

    It reveals that your pages are generated dynamically, and some search engines will refuse to index the pages at all. Other search engines will cut everything after the question mark from the URL.
    n
    The session ID will be added to users’ bookmarks and printouts.We even know of articles in technical journals that have the session ID of a Web site included as part of a reference. From a usability point of view, it’s harder for users to manually alter the URL to find specific resources on a site.
    n
    The session ID is logged in proxy servers and shows up in the HTTP_REFERER CGI environment variable for other sites.
    Dynamic Paths
    Let’s see if we can avoid some drawbacks of URL rewriting. For a start, you can add the ID to your URL in the Amazon.com way (see Figure 4.1) to make it look like
    http://server.com/page.php3/<session-id>.With this method, the session ID is part
    of the path to the script, and the URL looks like a static page to search engines and spiders.This works because the Web server knows that page.php3 is a script, and stops looking further in the URL for files. But this way the session ID is not automatically available in your PHP script.You need to parse the path yourself to get access to it:
    function session_start_from_path() {
    global $HTTP_HOST, $REQUEST_URI;
    ereg(“/([0-9a-z]{32})”, $REQUEST_URI, $regs); $session_id = $regs[1];
    if(!isset($session_id) || empty($session_id)) {
    srand((double)microtime()*1000000); $session_id = md5(uniqid(rand()));
    $destination = “http://$HTTP_HOST$REQUEST_URI/$session_id”; header(“Location: $destination”);
    }
    session_id($session_id); session_start();
    }
    All other drawbacks of URL rewriting still apply to dynamic paths, though.




    Dynamic Paths with mod_rewrite
    You can avoid at least the hassle of manually encoding the session ID with a clever trick.What if the URL looked like this?
    http://server.com/<session-id>/page.php3
    The browser would automatically send the session ID on every request, treating it as part of the directory. Of course, if you try to use this format as is, you’ll only get a File Not Found error, because there’s no directory that looks like the session ID.We need a way to remove the session ID from the path before the Web server actually sees the URL.
    This is where mod_rewrite comes into play.This is an Apache module that applies complex regular-expression transformations to a URL before passing it to the Apache server. Using mod_rewrite, we can simply strip out the session ID from the URL; this is an internal change to the URL, and only Apache will see it-the client won’t. Apache will see a normal request without session ID, while it’s still available in the usual variables for PHP.
    Getting mod_rewrite
    The mod_rewrite module is not compiled into Apache by default. Please see Apache’s INSTALL file for instructions on how to compile Apache with this module.

    129
    HTTP and Sessions
    An URL like this:
    http://www.server.com/b6ac8ca8e453cd...b5/script.php3
    can be rewritten with this rewriting rule:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^[0-9a-z]{32}/(.+) /$1
    The first line tells mod_rewrite to start up.The second line explicitly sets a base direc- tory, which is only needed when using mod_rewrite in a local context, for example in an .htaccess file.The third line finally defines the regular expression used for the URL rewriting. Our expression simply strips out the session ID from the URL.
    To start the session, we use a function very similar to the one we wrote earlier. Only the initial redirect is different:
    function session_start_from_rewrite() {
    global $HTTP_HOST, $REQUEST_URI;
    ereg(“/([0-9a-z]{32})”, $REQUEST_URI, $regs); $session_id = $regs[1];
    if(!isset($session_id) || empty($session_id)) {
    srand((double)microtime()*1000000); $session_id = md5(uniqid(rand()));
    $destination = “http://$HTTP_HOST/$session_id$REQUEST_URI”; header(“Location: $destination”);
    }
    session_id($session_id); session_start();
    }
    During all requests, the browser will assume the URL with the session ID included, and therefore send the session ID automatically with every request.This frees you from the hassle of rewriting all links yourself.That is, as long as you use only relative links in your application; if you use absolute URLs (for example, /directory/script.php3),
    you still need to rewrite those manually.
    Dynamic Paths with PHP 4.0
    Automatic URL rewriting is one of the very cool new features of PHP 4.0.To enable it, you need to configure PHP with --enable-trans-id and recompile it.Then the
    session ID in the form <session-name>=<session-id> will be added automatically to
    all relative links within your PHP-parsed pages.
    Not for High-Performance Sites
    While this is a handy feature, it should be used with caution on high-performance sites. PHP has to look at each individual page, analyze it to see whether it contains relative links, and eventually add the ID to the links. This obviously introduces a performance penalty. We recommend using mod_rewrite or DNS tricks instead.

    130
    Chapter 4 Web Application Concepts
    Details on session ID propagation in real life will follow a bit later. First, we’d like to show you another way of session ID propagation, arguably the most geeky method.
    DNS Tricks
    The need to tag all links in an application with the session ID can be really annoying. PHP 4.0 has a way to do it automatically, but it may be a severe performance hit on larger sites, and it doesn’t work with PHP 3.0.
    We may have a solution for you.
    Up front, the caveats:You need to be able to change the DNS record for your
    server, and the server you want to use for this kind of session ID propagation needs its own, static IP. Name-based virtual hosting won’t work here.
    You meet these requirements? Great. If you’re proficient with name servers, you may know that wildcard entries can be used in DNS configuration.These entries usually map any arbitrary hostname to a specific IP; for example, we’ve got this entry to direct requests for everything below phpwebdev.com to the IP 194.242.199.228:
    *.phpwebdev.com IN A 194.242.199.228
    A request for http://this.is.one.cool.domain.phpwebdev.com will be redirected to
    the specified IP. Since the hostname is arbitrary, Apache must be configured to handle the IP-as opposed to name-based virtual hosting, where the hostname must be fixed and known. Our Apache configuration looks like this:
    <VirtualHost 194.242.199.228>
    ServerAdmin tobias@dev.phpwebdev.com DocumentRoot /home/www/htdocs ServerName phpwebdev.com
    </VirtualHost>
    Our trick will also work fine if Apache’s main server is bound to this address. The scope of this is of course to encode the session ID in the hostname itself. On
    the first request to the application, the session ID is created, and the client is redirected to the new URL containing the tagged hostname, which will look like this:
    355e1bce8828d4fb5c83c1e35ad02caa.phpwebdev.com
    The advantage is clear: As long as you use relative links in your application, it’s no longer necessary to bother with any manual URL rewriting!
    We have modified the earlier session start function to extract the session ID from the hostname:
    function session_start_from_host($host) {
    global $HTTP_HOST, $PHP_SELF;
    ereg(“([0-9a-z]{32})\.”, $HTTP_HOST, $regs); $session_id = $regs[1];

    131
    HTTP and Sessions
    if(!isset($session_id) || empty($session_id)) {
    srand((double)microtime()*1000000); $session_id = md5(uniqid(rand()));
    $destination = “http://$session_id.$host$PHP_SELF”; header(“Location: $destination”);
    }
    session_id($session_id); session_start();
    }
    Last edited by bigpaul; April 19th, 2003 at 11:47 PM.

IMN logo majestic logo threadwatch logo seochat tools logo