...

View Full Version : making clean urls using PHP ??



PHPycho
09-04-2007, 05:14 AM
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.

goheadtry
09-04-2007, 05:30 AM
I always use mod_rewrite and rewrite the url but I edit the script so it outputs the correct urls also

PHPycho
09-04-2007, 05:49 AM
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.

madmatter23
09-04-2007, 06:40 AM
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:


RewriteEngine on


Then tack on your rewrite rules afterwards. This might not be exactly right, but you're looking for something like this:


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.

PHPycho
09-04-2007, 09:41 AM
how to enable $_SERVER['PATH_INFO'], it says undefined index PATH_INFO.

madmatter23
09-04-2007, 07:49 PM
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


$url = $_GET["url"];


Then you know that the full URL is:


$wholeURL = "http://localhost/my_project/".$url


If you really wanted to figure it out using $_SERVER strings, you could try:


$theUserTyped = 'http://'. $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];


Let me know if that all works out.

PHPycho
09-05-2007, 05:38 AM
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:


RewriteEngine on


Then tack on your rewrite rules afterwards. This might not be exactly right, but you're looking for something like this:


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

goheadtry
09-07-2007, 06:15 AM
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

Eli.
09-07-2007, 03:59 PM
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.

syosoft
09-09-2007, 01:10 PM
.htaccess
RewriteEngine On
RewriteRule ^projects/(.*)/(.*)$ /projects.php?file=$1&action=$2 [QSA,NC,L]



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

include 'content/'.$file;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum