tags:

views:

162

answers:

3

I'm trying to do clean urls by exploding $_SERVER['REQUEST_URI'] and then switching between the results.

However, once a user goes outside index.php, I'm assuming I need to redirect them back to index.php in order to process the URL they want to reach. How can I accomplish this?

So for instance, user goes to www.domain.com/home/johndoe/... i'd like the index.php (domain.com/index.php) to be hit so that it can process the /home/johndoe/ via request_uri.

+1  A: 

You can use apache's mod_rewrite to map urls based on regular expressions, including sending everything to index.php

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

Alex Sexton
Never really liked the apache docs... too wordy when you are looking for a specific thing
SeanJA
+8  A: 

.htaccess:

RewriteEngine on
RewriteBase /
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?param=$1 [QSA,L]
Col. Shrapnel
@pixel: This is the most common way of doing it. If a file or directory is not found by apache, it will call call index.php. Now you can access the requested uri in your index.php with `$_GET['param']`. Note: on certain setups `RewriteBase /` can cause problems. If so, just comment it out with a pound sign.
webbiedave
Exactly what I was looking for. Thanks!
pixel
A: 

You will need images, and other files what will show up in the index.php.

RewriteEngine On

Turns on the Rewite Engine

RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|gif|png|css|js|pl|txt)$

If the filename is not equal JPG, JPEG... which need for index.php

RewriteRule ^(.*)$ index.php?q=$1 [QSA]

"Redirect" to index.php.

In use:

With PHP $_GET the q will give you the /articles/1987/12/20/Répa's birthday/

If you split the variable in every slash with

list($type, $date_year, $date_month, $date_day, $title) = split('[/-]', $_GET['q']);

You will got exactly what you want.

neduddki
That rewrite condition is a bit excessive, and will break things if you end up saving files without file extensions (which could definitely happen).
SeanJA