Reputation: 145
I am new about this redirection and I want to know how it works? I have some file called index.php and i want to hadlle it any directory in the site
index.php
<?php
if(empty($dir)){
include('includes/home.php');
}
if($dir=='profile'){
include('includes/profile.php');
}
elseif($dir=='settings'){
include('includes/settings.php');
}
else{
include('includes/404.php');
}
?>
The url is:
test 1. www.example.com/ - will view the include home.
test 2. www.example.com/settings - will view the include settings.
test 3. www.example.com/errorsample - will view the include 404 page.
How to make .htaccess
and index.php using that code or any idea how it works and sample code of it.
Upvotes: 1
Views: 201
Reputation: 8706
This isn't really redirection. What you seem to be describing is a rudimentary page controller - a single point of entry into your website framework which can react to different requests.
The first thing you need to do with this is think about your site folder structure - typically, you'd have a "public" directory and a "library" (or "include") directory on the same level. This means that, as long as you set the DocumentRoot
in your apache config to /var/www/yoursite/public
- it will be harder for people to get to your PHP code in the "include" directory at /var/www/yoursite/include
.
The public directory will then contain your .htaccess and index.php files.
The simplest setup is to then have this in your .htaccess:
RewriteEngine On
RewriteRule ^(.*)$ /index.php?page=$1 [L,QSA]
This captures the URL sent to website, and sets as a value in the PHP $_GET array. The QSA option here means that if your links have a querystring (something like http://www.example.com/module?parameter=value), then this information is also passed on to your script.
I think you will find it tedious (and rapidly unmaintainable) to create a long list of if/else or even a long switch/case block - and ultimately, this one-to-one matching of URLs and files isn't going to be helpful as you might as well have just left the files available in the public directory. Of course, the power of using page controllers (or full Model-View-Controller) is that you don't have to have this one-to-one match: you can pass IDs or even "friendly" URLs which can then be used to extract stored information from a database (like blog posts, etc).
Also - you should be careful in mapping URLs to the include
function - what if someone were to request http://www.example.com/database.php ? - this could really start messing up your site and server if you haven't taken suitable precautions.
Upvotes: 0
Reputation: 79049
I will try to explain this with a simple example. Consider the following .htaccess
file
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ /index.php?x=$1 [L]
# ^ This is the rule that does the redirection
What it does is, it routes every url request and sends the request to index.php. How it send it? I will show couple of example to do that.
www.example.com/settings
will be send as www.example.com/index.php?x=settings
www.example.com/errorsample
will be send as www.example.com/index.php?x=errorsample
So, now you are configure your index.php and decided what you want to do, with the value you get in $_GET['x']
switch($_GET['x']) {
case "profile": include("include/profile.php"); break;
// .... similarly other cases
default: include("includes/home.php"); break;
}
}
Upvotes: 2