I have a small website. It's .htaccess
file is like this:
RewriteEngine On
RewriteBase /site/
RewriteRule ^(.+)$ index.php [QSA,L]
So it redirects all the URLs to 'index.php'. I can get the requested URL and act accordingly :
$uri = $_SERVER['REQUEST_URI'];
switch($uri)
{
case 'index':
LoadIndex();
break;
case 'about':
LoadAbout();
break;
case 'Posts':
LoadPosts();
break;
default:
LoadNotFound();
}
Say I want to use $_GET[]
in Index page. That changes the URL, so it fails to load the page. How can I do that? How can I route my site without affecting $_GET[]
variables in URLs?
$_SERVER[REQUEST_URI]
will be /index.php
and not index
. $_SERVER[REQUEST_URI]
also includes the QUERY_STRING
. So, it might be /index.php?var1=abc&var2=def
.
If you need only the URI path, try PHP_SELF
or SCRIPT_NAME
. But keep in mind, that these will be /index.php
too, including /
and .php
.
$uri = $_SERVER['PHP_SELF'];
switch($uri)
{
case '/index.php':
LoadIndex();
break;
...
}
You don't need QSA
in your RewriteRule
. From RewriteRule Directive
Modifying the Query String
By default, the query string is passed through unchanged.
This means, the $_GET
variable is available in your PHP script as before.