阿帕奇的.htaccess:我如何重定向文件是不是没有发送404头发现了什么? [英] Apache .htaccess: How do I redirect for files that aren't found without sending a 404 header?

查看:194
本文介绍了阿帕奇的.htaccess:我如何重定向文件是不是没有发送404头发现了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站需要一个htaccess文件,它将在找不到页面将用户重定向到index.php文件。不过,我不希望阿帕奇发送404头的文件。

我问这个问题前面: 的Apache的.htaccess重定向

命令的ErrorDocument的index.php产生,我想,除了它发送一个404头与页面的确切疗效。我能做什么?我应该覆盖404头用PHP?

谢谢你们。

解决方案

添加到您的的.htaccess 文件:

  RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME -s [OR]
的RewriteCond%{} REQUEST_FILENAME -l [OR]
的RewriteCond%{} REQUEST_FILENAME -d
。重写规则^ * $  -  [NC,L]
重写规则^ * $的index.php [NC,L]
 

  1. 使重写
  2. 检查请求的文件存在,作为一个regualar文件大小(不为空)
  3. 检查请求的文件链接
  4. 检查请求的文件是目录
  5. 如果的previous 3语句之一,则显示该文件
  6. 否则转到到index.php

如果重定向到index.php发生的美国可以通过使用获得请求的URI $ _ SERVER [REQUEST_URI] 的index.php

  
      
  • 在 - D'(就是目录)将TestString视为一个路径名并测试   它存在且是一个目录。
  •   
  • 在' - F'(是常规文件)将TestString视为一个路径名并测试   它的存在,是一个普通文件。
  •   
  • 在' - S'(与大小的常规文件)将TestString视为一个路径名   并测试它是否存在,是一个   大小大于常规文件   零。
  •   
  • 在' - L'(是符号连接)将TestString视为一个路径名并测试   它的存在,是一个符号链接。
  •   
  • 在' - F'(是现有对子请求文件)检查TestString是否一个   有效的文件,并通过所有可访问   服务器的当前配置的访问   控制了这条道路。它使用一个   内部子请求来确定   检查,所以请谨慎使用,因为它   降低服务器的性能!
  •   
  • 在' - U'(是现有对子请求的URL)检查TestString是否一个   有效的URL,并通过所有可访问   服务器的当前配置的访问   控制了这条道路。它使用一个   内部子请求来确定   检查,所以请谨慎使用,因为它   降低服务器的性能!
  •   

详细信息:<一href="http://httpd.apache.org/docs/current/mod/mod_rewrite.html">http://httpd.apache.org/docs/current/mod/mod_rewrite.html

编辑:也请注意这个帖子: mod_rewrite的与godaddy

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.

I asked this question earlier: Apache .htaccess redirect

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?

Thanks guys.

解决方案

Add this to your .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

  1. enable rewrite
  2. check if requested file exists as a regualar file with size (not empty)
  3. check if requested file is link
  4. check if requested file is a directory
  5. if one of the previous 3 statements is true show that file
  6. otherwise go to index.php

If the redirect to index.php happens u can get the requested uri by using $_SERVER["REQUEST_URI"] inside index.php

  • '-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!

More information: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Edit: please also notice this post: Mod_rewrite with godaddy

这篇关于阿帕奇的.htaccess:我如何重定向文件是不是没有发送404头发现了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆