简体   繁体   English

使用SERVER ['REQUEST_URI']和GET控制网站流量

[英]Using SERVER['REQUEST_URI'] and GET in controlling the website flow

I have a small website. 我有一个小网站。 It's .htaccess file is like this: 它的.htaccess文件是这样的:

RewriteEngine On
RewriteBase /site/
RewriteRule ^(.+)$ index.php [QSA,L]

So it redirects all the URLs to 'index.php'. 因此,它将所有URL重定向到“ index.php”。 I can get the requested URL and act accordingly : 我可以获取请求的URL并采取相应的行动:

$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. 假设我要在索引页面中使用$_GET[] That changes the URL, so it fails to load the page. 这会更改URL,因此无法加载页面。 How can I do that? 我怎样才能做到这一点? How can I route my site without affecting $_GET[] variables in URLs? 如何在不影响URL中$_GET[]变量的情况下路由站点?

$_SERVER[REQUEST_URI] will be /index.php and not index . $_SERVER[REQUEST_URI]将是/index.php不是 index $_SERVER[REQUEST_URI] also includes the QUERY_STRING . $_SERVER[REQUEST_URI]还包括QUERY_STRING So, it might be /index.php?var1=abc&var2=def . 因此,它可能是/index.php?var1=abc&var2=def

If you need only the URI path, try PHP_SELF or SCRIPT_NAME . 如果仅需要URI路径,请尝试PHP_SELFSCRIPT_NAME But keep in mind, that these will be /index.php too, including / and .php . 但是请记住,它们也将是/index.php ,包括/.php

$uri = $_SERVER['PHP_SELF'];
switch($uri)
{
case '/index.php':
    LoadIndex();
    break;
...
}

You don't need QSA in your RewriteRule . 您的RewriteRule不需要QSA From RewriteRule Directive 来自RewriteRule指令

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. 这意味着$_GET变量可以像以前一样在您的PHP脚本中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM