Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
.htaccess 如何将除一个url之外的所有url重定向到脚本_.htaccess_Mod Rewrite - Fatal编程技术网

.htaccess 如何将除一个url之外的所有url重定向到脚本

.htaccess 如何将除一个url之外的所有url重定向到脚本,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我正在尝试让www.example.com和www.example.com/index.html转到index.html,但我希望所有其他URL,例如www.example.com/this/is/other/link,仍然显示www.example.com/this/is/other/link,但由通用脚本处理。我试过了 RewriteEngine on RewriteCond %{REQUEST_URI} !^index\.html$ RewriteCond %{REQUEST_URI} !

我正在尝试让www.example.com和www.example.com/index.html转到index.html,但我希望所有其他URL,例如www.example.com/this/is/other/link,仍然显示www.example.com/this/is/other/link,但由通用脚本处理。我试过了

RewriteEngine on
RewriteCond %{REQUEST_URI} !^index\.html$
RewriteCond %{REQUEST_URI} !^$
RewriteRule ^(.*)$ mygenericscript.php [L]

但是它不起作用,有人能帮忙吗?

最简单的方法是安装一个404处理程序,当服务器找不到要显示的文件时,该处理程序就会执行

ErrorDocument 404 /mygenericscript.php

或者类似的方法就可以了


这并不是说RewriteRule不能用于此目的,只是它们的设置很棘手,需要深入了解apache如何处理请求。这有点像黑色艺术。

最简单的方法是安装一个404处理程序,在服务器找不到要显示的文件时执行该处理程序

ErrorDocument 404 /mygenericscript.php

或者类似的方法就可以了


这并不是说RewriteRule不能用于此目的,只是它们的设置很棘手,需要深入了解apache如何处理请求。这有点像黑色艺术。

它看起来像是在使用PHP,您可以使用auto_x_文件(x是append或prepend:


它看起来像是在使用PHP,您可以使用auto_x_文件(x是append或prepend:


您可以只测试资源是否存在,而不是测试
%{REQUEST\u URI}
是什么:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* mygenericscript.php
如果静态资源(图像、样式表等)通过
.htaccess
所在的同一目录进行处理,这将防止它们被重定向


现在可能发生的情况是,当您尝试访问任何不是
/
/index.html
的内容时,您会看到一个内部服务器错误,这是因为
*
匹配每个请求,在您第一次重写到
mygenericscript.php
之后重新处理规则集(因为
mod_rewrite
在您使用它的上下文中是如何工作的)。

您可以只测试资源是否存在,而不是测试
%{REQUEST\u URI}
是什么:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* mygenericscript.php
如果静态资源(图像、样式表等)通过
.htaccess
所在的同一目录进行处理,这将防止它们被重定向

现在可能发生的情况是,当您尝试访问任何不是
/
/index.html
的内容时,您会看到一个内部服务器错误,这是因为
*
匹配每个请求,在您第一次重写到
mygenericscript.php
之后规则集被重新处理(因为
mod_rewrite
在您使用它的上下文中是如何工作的)