Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 如何使用mod_rewrite将PHP页面以外的任何内容定向到index.PHP?_Apache_Url_Mod Rewrite_Redirect - Fatal编程技术网

Apache 如何使用mod_rewrite将PHP页面以外的任何内容定向到index.PHP?

Apache 如何使用mod_rewrite将PHP页面以外的任何内容定向到index.PHP?,apache,url,mod-rewrite,redirect,Apache,Url,Mod Rewrite,Redirect,我有一个问题有点困惑。我试图理解mod_重写,但我被它搞糊涂了 我试图做的是将所有URL(如/settings/account或/user/user123)重定向到index.php。一个PHP脚本(我已经创建)使用$\u SERVER['REQUEST\u URI']将URL分解为若干部分,然后使用include。例如,它将包括settings.php?page=account或user.php?uid=user123 但如果URL是/settings.php?page=account或/us

我有一个问题有点困惑。我试图理解mod_重写,但我被它搞糊涂了

我试图做的是将所有URL(如/settings/account或/user/user123)重定向到index.php。一个PHP脚本(我已经创建)使用$\u SERVER['REQUEST\u URI']将URL分解为若干部分,然后使用include。例如,它将包括settings.php?page=account或user.php?uid=user123

但如果URL是/settings.php?page=account或/user.php?uid=user123,我不希望它通过索引重定向。换句话说,如果URl有文件扩展名,只需转到该文件,但如果它没有(如/settings),则转到index.php处理它应该去的地方


如何做到这一点?提前感谢您的建议

如果你不想使用普通的方法,你也可以试试你的方法。要将任何不带点的内容重定向到公共脚本,请使用:

 RewriteRule  ^([^.]*)$  index.php?path=$1
您可能仍希望排除某些URL,如
图像/
css/
等。在规则之前使用简单的重写秒,然后:

 RewriteCond  %{REQUEST_URI}  !images/
 RewriteCond  %{REQUEST_URI}  !css/
您需要的是带有
的指令-f
-d
模式

RewriteCond
检查用户的HTTP请求是否与提供的条件匹配。在您的情况下,您需要使用
检查您的文档根目录上是否已经存在请求的文件(在变量
%{REQUEST\u FILENAME}
中提供)-f
postfix。同样,如果要在目录中建立索引,可以使用
添加相同的模式-d
,它将检查请求的目录是否位于文档根目录中

因此,在实际的
重写规则
之前的这两行将起作用:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d