tags:

views:

1572

answers:

7

Hi, normally, the practice or very old way of displaying some profile page is like this: www.domain.com/profile.php?u=12345

where u=12345 is the user id.

recent years, I found some website, their url very nice, its like: www.domain.com/profile/12345

How do I do this in php? Please teach me, thanks

EDIT:

Just a wild guess, is it something to do with the .htaccess file? Can you give me more tips or some sample code on how to write the .htaccess file?

+7  A: 

See the fine article:

http://www.phpriot.com/articles/search-engine-urls

It explains that yeah, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:

RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1

And this maps requests from

/news.php?news_id=63

to

/news/63.html

Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:

<Files news>
    ForceType application/x-httpd-php
</Files>

And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:

<?php
    echo $_SERVER['PATH_INFO'];
    // outputs '/63.html'
?>
altCognito
A great mod_rewrite resource: http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/
James Socol
I'd suggest his tutorial before the cheatsheet: http://www.addedbytes.com/apache/url-rewriting-for-beginners/
Chad Birch
Another option, if you want to do your URL rewriting entirely PHP, is to set rewrite rules to direct all requests to a single script. One great advantage of this, is you can store your code in an area not accessible to Apache, and only your URL dispatching script need be Apache-accessible. e.g.:RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . dispatch.phpThis is similar to your forcetype example.
Frank Farmer
A: 

It's actually not PHP, it's apache using mod_rewrite. What happens is the person requests the link, www.example.com/profile/12345 and then apache chops it up using a rewrite rule making it look like this, www.example.com/profile.php?u=12345, to the server. You can find more here: Rewrite Guide

jacobangel
A: 

The subject your question relates to is url rewriting . Check here for a tutorial on how to do that http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

A: 

It looks like you are talking about a RESTful webservice.

http://en.wikipedia.org/wiki/Representational_State_Transfer

The .htaccess file does rewrite all URIs to point to one controller, but that is more detailed then you want to get at this point. You may want to look at Recess

It's a RESTful framework all in PHP

ryanday
Technically I suppose you are right, but unless the OP is already experienced with the REST idea, even reading that Wikipedia article is far more effort than the problem needs. Invoking a whole programming paradigm is overkill for this situation.
David Zaslavsky
Really? I don't think his questions was exactly "How do I make profile.php?u=12345 goto /profile/12345", I interpreted that as an overall "How is this done in production environments?". I think it is beneficial to know that there IS a paradigm out there, and not have a .htaccess file full of redirects when there is a proper way to address that problem.
ryanday
+4  A: 

I recently used the following in an application that is working well for my needs.

.htaccess

<IfModule mod_rewrite.c>
# enable rewrite engine
RewriteEngine On

# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
</IfModule>

index.php

foreach (explode ("/", $_SERVER['REQUEST_URI']) as $part)
{
    // Figure out what you want to do with the URL parts.
}
Jordan S. Jones
A: 

ModRewrite is not the only answer. You could also use Options +MultiViews in .htaccess and then check $_SERVER REQUEST_URI to find everything that is in URL.

empi
Yeah, but MultiViews is icky... I'd say it's too general for this usage.
David Zaslavsky
A: 

There are lots of different ways to do this. One way is to use the RewriteRule techniques mentioned earlier to mask query string values.

One of the ways I really like is if you use the front controller pattern, you can also use urls like http://yoursite.com/index.php/path/to/your/page/here and parse the value of $_SERVER['REQUEST_URI'].

You can easily extract the /path/to/your/page/here bit with the following bit of code:

$route = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));

From there, you can parse it however you please, but for pete's sake make sure you sanitise it ;)

Shabbyrobe