6

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, if the user goes to www.domain.com/home/johndoe/... I'd like index.php (domain.com/index.php) to be hit so that it can process the /home/johndoe/ via request_uri.

3 Answers 3

19

.htaccess:

RewriteEngine on
RewriteBase /
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?param=$1 [QSA,L]
Sign up to request clarification or add additional context in comments.

1 Comment

@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.
2

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.

1 Comment

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

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

1 Comment

Never really liked the apache docs... too wordy when you are looking for a specific thing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.