Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Catch-All Handler in PHP?

I want to have a PHP file catch and manage what's going to happen when users visit:

http://profiles.mywebsite.com/sometext

sometext is varying.

E.g. It can be someuser it can be john, etc. then I want a PHP file to handle requests from that structure.

My main goal is to have that certain PHP file to redirect my site users to their corresponding profiles but their profiles are different from that URL structure. I'm aiming for giving my users a sort of easy-to-remember profile URLs.

Thanks to those who'd answer!

like image 715
Registered User Avatar asked Mar 07 '11 09:03

Registered User


1 Answers

Either in Apache configuration files [VirtualHost or Directory directives], or in .htaccess file put following line:

Options -MultiViews

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L,NC,QSA]
</IfModule>

It will silently redirect all incoming requests that do not correspond to valid filename or directory (RewriteCond's in the code above make sure of that), to index.php file. Additionally, as you see, MultiViews option also needs to be disabled for redirection to work - it generally conflicts with these two RewriteCond's I put there.

Inside index.php you can access the REQUEST_URI data via $_SERVER['REQUEST_URI'] variable. You shouldn't pass any URIs via GET, as it may pollute your Query-String data in an undesired way, since [QSA] parameter in our RewriteRule is active.

like image 90
Shahriyar Imanov Avatar answered Sep 25 '22 01:09

Shahriyar Imanov