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

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

10 thoughts on “Use PHP to handle all incoming URL requests in a SEO friendly manner”

  1. $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’]);

    1. 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.

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

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

  4. 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.

  5. Pingback: Apache rewriting rules with several conditions |

  6. Pingback: Url rewriting except for one folder |

  7. Pingback: Url rewriting except for one folder - Admins Goodies

Comments are closed.

Scroll to Top