Use PHP to handle all incoming URL requests in a SEO friendly manner

by Yang Yang on February 23, 2010

in PHP Tips & Tutorials

Share This Article:
Subscribe to Kavoir: blog feed

While you can always use .htaccess and the mod_rewrite module to map SEO friendly URLs to actual PHP parameterized URLs with question marks and ampersands, you can simply put these lines in .htaccess and then rely on PHP entirely to recognize and handle all incoming URL requests of any kind / form:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f # ignoring any existing files
RewriteCond %{REQUEST_FILENAME} !-d # ignoring any existing directories
RewriteRule . /index.php [L]

</IfModule>

The index.php file in the document root of your domain is now effectively in charge of all incoming URL requests. You can recognize / distinguish the requests and perform accordingly in your PHP script, the key is the PHP environmental variable $_SERVER["REQUEST_URI"] which contains the complete URL request string. Consider a index.php like this:

<?php // index.php

$req = explode('/', $_SERVER['REQUEST_URI']);
print_r($req);

?>

Type in your browser address bar:

http://www.example.com/user/110/edit

And the site will render the output page like this:

Array
(
    [0] =>
    [1] => user
    [2] => 110
    [3] => edit
)

So you can process the request according to the array. It probably means opening the editor page to edit the user whose ID is 110. Just sub-include the responsible PHP script to get the job done from index.php. To parse more complicated request URLs, e.g. with question marks and parameters, you may want to use the parse_url() or preg_split().

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

{ 7 comments… read them below or add one }

OIS February 24, 2010 at 11:13 pm

$req = preg_split(‘/\//’, $_SERVER['REQUEST_URI']);
seriously?
$req = explode(‘/’, $_SERVER['REQUEST_URI']);

and in the future, you should rather use something like # as delimiter instead of /, especially when you use / in the regex itself
$req = preg_split(‘#/#’, $_SERVER['REQUEST_URI']);

Reply

Yang Yang February 25, 2010 at 10:59 am

Thanks for the nice tip. The snippet was from one of my old projects and the original regular expression was more complicated than what I used here – I just simplified it without switching to a better function.

Reply

n0xie February 26, 2010 at 8:00 am
tanuj March 25, 2010 at 9:28 pm

thnks for this information

Reply

Kevin Doug April 1, 2010 at 1:14 am

Hey there,

Thank you very much. However, I am facing an issue here, it shows me “NOT FOUND” page instead of redirecting it to the original index.php

Reply

Bryan April 5, 2010 at 1:10 am

I used your code above and did some minor changes using OIS advice and it worked great. Thanks!

Reply

Majid July 4, 2010 at 3:23 am

Hi Mate, I was looking for something like this.

I see people only had two like of code in .htaccess file but they site is SEO Friendly.

I wont to learn how i can do that, I seen people create another attribute in they database which is unique. hen domain.com/name

but also they have like domain.com/page/name
domain.com/tag/name

using two like of code in they htaccess

I seen these line in htaccess

RewriteCond %{REQUEST_FILENAME} !-f # ignoring any existing files
RewriteCond %{REQUEST_FILENAME} !-d # ignoring any existing directories

Can someone please explain how i can do that. I’m learning to develop in PHP and i wont to make site in SEO. rather then domian.com?id=8&name=something

most search engine ignore these as they don’t count them as links.

Please help.

Reply

Leave a Comment

{ 3 trackbacks }

Previous post:

Next post: