开发者

Create a Catch-All Handler in PHP?

开发者 https://www.devze.com 2023-02-15 18:39 出处:网络
I want to have a PHP file catch and manage what\'s going to happen when users visit: http://profiles.mywebsite.com/sometext

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 the开发者_JS百科ir 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!


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.


You should use a rewrite rule..

In apache (.htaccess), something like this:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>

Then in your index.php you can read $_GET['url'] in your php code.


You can use a .htaccess file (http://httpd.apache.org/docs/1.3/howto/htaccess.html) to rewrite your url to something like profiles.websites.com/index.php?page=sometext . Then you can do what you want with sometext in index.php.


An obvious way to do this would be via the 404 errorDocument - saves all that messing about with mod_rewrite.


If you have not heard about MVC, its time you hear it, start with CodeIgniter, its simplest and is quite fast, use default controller and you can have URLs like
domain.com/usernam/profile
domain.com/usernam/profile/edit
domain.com/usernam/inbox
domain.com/usernam/inbox/read/messageid Or use .htaccess wisely

0

精彩评论

暂无评论...
验证码 换一张
取 消