views:

49

answers:

3

Hello,

When I click on a comment section for a given entry on a site I have, the URL looks like this:

http://www...com/.../comments/index.php?submission=Portugal%20Crushes%20North%20Korea&submissionid=62&url=nytimes.com/2010/06/22/sports/soccer/22portugalgame.html?hpw&countcomments=3&submittor=johnjohn12&submissiondate=2010-06-21%2019:00:07&dispurl=nytimes.com

I want to make it look like this URL:

http://www...com/.../comments/Portugal-Crushes-North-Korea-62

I understand that this involves adding rules to the .htaccess file. I have two questions:

  1. Since I am using the GET method in PHP, the ugly URL has a bunch of variables appended to it. I don't want all of these variables to appear in the clean URL. Is it possible to only include a few of the variables in the clean URL but still have a rule directing it to an ugly URL with all of the variables?

  2. Once I have the .htaccess rules written, do I go back and change the links in the source code to direct to the clean URLs? If so, how do I do this using the GET method when the clean URL does not have all of the variables that I want to pass along?

Thanks in advance,

John

A: 

No, you can not leave variables out and expect them to be passed anyway. If you do this, the information is no longer in the URL, so you don't have a way to get it.

You can use post instead of get if you want to pass variables without them showing up in the URL.

Sjoerd
How do I do POST when the way to get to the comments section is just a hyperlink?
John
@John: You can't.
Johannes Gorset
+2  A: 

I'm not sure why you need all that data in the URL. You should be storing things like the submission title, its date and author in a database and then refer to it with an ID. That way, your URLs will be shorter and prettier:

http://www.example.org/article.php?id=1

http://www.example.org/article/1/

You can accomplish this with a simple RewriteRule in your .htaccess file, like so:

RewriteEngine On
RewriteRule ^articles/([0-9]+)/    article.php?id=$1
Johannes Gorset
Thanks... I actually came up with this idea myself before I saw your comment. I think I am going to go this route.
John
+1  A: 

I join the word of Sjoerd, but there are a lot of ways how you can rewrite your url like you want to!

Apache and (IIS too) supports url-s like this one: http://example.com/index.php/my-rewritten-url_62

function URISegment($segment)
{
    $uri_array = explode('/',$_SERVER['REQUEST_URI']);
    $uri_count = count($uri_array);
    $returning_uri = array();

    for($i = 0;$i<$uri_count;$i++)
    {
        if(empty($uri_array[$i]) || $uri_array[$i] == "index.php")
            unset($uri_array[$i]);
        else
            array_push($returning_uri,$uri_array[$i]);
    }

    if($segment < count($returning_uri))
        return $returning_uri[$segment];
    else
        return false;
}

This works, but you need to define the base url too, and this needs to be called at the beginning of the file, and implemented at every image, script, etc. call.

function BaseURL()
{
    if(isset($_SERVER['HTTP_HOST']))
    {
        $base = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
        $base .= '://'. $_SERVER['HTTP_HOST'];
        $base .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
    }
    else
    {
        $base = 'http://localhost/';
    }
    return $base;   
}

After this you can use instead of this:

// http://example.com/?MyKey=Some-data
$MyKey = $_GET['MyKey']; //which is the first item
echo $MyKey;
// results: Some-data

This:

// http://example.com/?MyKey=Some-data
$MyKey = URISegment(0);
echo $MyKey;
// results: Some-data

You've got the same result by each one.

PS:

I like this solution because I can mix url types as I need them like:

example.com/index.php/index/evaled-article?some=db-stored&code=snipplet

And of course you can rewrite your url like FRKT said :)

And of course, if you want to hide the index.php you need to use mod_rewrite, because there's no way

Nort
That's not very pretty. Why not use mod_rewrite (or IIS' equivalent)?
Johannes Gorset
Why isn't it pretty? It is a pretty solution for the problem he listed. I think its a very useful way of url rewriting, and the other thing, now he has a new aspect how this can be done, using only php
Nort
I'm not sure this falls into the category of URL rewriting, but I agree it's a good alternative for people who can't use mod_rewrite. :-)
Johannes Gorset