如何重定向未发送404标头找不到的URL? [英] How do I redirect a URL that isn't found without sending a 404 header?
问题描述
我的网站需要一个 .htaccess
文件,该文件将在没有页面的情况下将用户重定向到 index.php
找到了。但是,我不希望Apache随文档发送404标头。
My website needs a .htaccess
file that will redirect a user to index.php
when a page is not found. However, I do not want Apache to send a 404 header with the document.
我之前问过这个问题:
Apache .htaccess 404错误重定向
I asked this question earlier: Apache .htaccess 404 error redirect
命令 ErrorDocument /index.php
产生我想要的确切效果,除了它会在页面中发送404标头。我能做什么?我应该用PHP覆盖404标头吗?
The command "ErrorDocument /index.php"
produces the exact effect that I want, except that it sends a 404 header with the page. What can I do? Should I overwrite the 404 header with PHP?
推荐答案
将此添加到您的 .htaccess
文件:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
- 启用重写
- 检查请求的文件是否以大小不固定的常规文件存在(不为空)
- 检查请求的文件是否为链接
- 检查请求的文件是否为链接目录
- 如果前三个语句之一为true,则表明该文件
- 否则转到index.php
- enable rewrite
- check if requested file exists as a regualar file with size (not empty)
- check if requested file is link
- check if requested file is a directory
- if one of the previous 3 statements is true show that file
- otherwise go to index.php
如果发生重定向到index.php的操作,您可以使用 $ _ SERVER [ REQUEST_URI]
获取请求的uri。内部 index.php
If the redirect to index.php happens u can get the requested uri by using $_SERVER["REQUEST_URI"]
inside index.php
- '-d' (是目录)对待TestString作为路径名并测试
是否存在并且是目录。
- '-f'(是常规文件)将TestString视为路径名并测试
它存在并且是常规文件。
- '-s'(是具有大小的常规文件)将TestString视为路径名
并测试它是否存在并且是
常规文件,其大小大于
零。
- '-l'(符号链接)将TestString视为路径名并测试
是否存在,以及是符号链接。
- '-F'(通过subrequest是现有文件)检查TestString是否为
有效文件,并且可以通过所有
服务器当前的访问权限访问-已配置该路径的访问
控件。这将使用
内部子请求来确定
支票,因此请谨慎使用,因为
会降低服务器性能!
- '-U'(是通过subrequest的现有URL)检查TestString是否为
有效URL,并且可以通过该路径的所有
服务器当前配置的访问
控件进行访问。这使用
内部子请求来确定
支票,因此请谨慎使用,因为
会降低服务器的性能!
- '-d' (is directory) Treats the TestString as a pathname and tests if it exists and is a directory.
- '-f' (is regular file) Treats the TestString as a pathname and tests if it exists and is a regular file.
- '-s' (is regular file with size) Treats the TestString as a pathname and tests if it exists and is a regular file with size greater than zero.
- '-l' (is symbolic link) Treats the TestString as a pathname and tests if it exists and is a symbolic link.
- '-F' (is existing file via subrequest) Checks if TestString is a valid file and accessible via all the server's currently-configured access controls for that path. This uses an internal subrequest to determine the check, so use it with care because it decreases your servers performance!
- '-U' (is existing URL via subrequest) Checks if TestString is a valid URL and accessible via all the server's currently-configured access controls for that path. This uses an internal subrequest to determine the check, so use it with care because it decreases your server's performance!
更多信息: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
这篇关于如何重定向未发送404标头找不到的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!