Categories
PHP Tips & Tutorials

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]

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

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.

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

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

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.

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

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.

Comments are closed.