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!
You should use a rewrite rule..
In apache (.htaccess), something like this:
Then in your index.php you can read $_GET['url'] in your php code.
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 wiselyEither in Apache configuration files [VirtualHost or Directory directives], or in .htaccess file put following line:
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 twoRewriteCond
'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 viaGET
, as it may pollute your Query-String data in an undesired way, since[QSA]
parameter in our RewriteRule is active.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.