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

by Yang Yang on February 23, 2010

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
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


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']);


Type in your browser address bar:

And the site will render the output page like this:

    [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().

OIS February 24, 2010 at 11:13 pm

$req = preg_split(‘/\//’, $_SERVER[‘REQUEST_URI’]);
$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’]);

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.

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

thnks for this information

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

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!

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

but also they have like

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

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

Please help.

Comments on this entry are closed.

{ 3 trackbacks }

Previous post:

Next post: