.htaccess url-rewrite 如果文件不存在

标签 .htaccess mod-rewrite url-rewriting file-exists

我必须为一个网站做点小 Action ! 想法是:

  • 如果所需 url 的文件存在,那么我会转到该 url,不执行任何操作;
  • 如果所需 url 的文件不存在,我必须转到 file.php 并执行某些操作,但不更改 url!

示例:

www.mysite.com/page1.htm -> 存在 -> 转到文件 page1.htm

www.mysite.com/page2.htm -> 不存在 -> 转到文件default.php,但网址为“www.mysite.com/page2.htm”

可以通过 .htaccess 来完成这一切吗?

最佳答案

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /default.php [L]

关于.htaccess url-rewrite 如果文件不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5469955/

相关文章:

php - 处理40 4's with Named Location in Nginx and returning custom 404'

php - 使用 .htaccess 将所有 URL 定向到一页

apache - htaccess重写: Grab text after last slash as parameter

Apache .htaccess : Serving . css 文件来自单独的域?

apache - 重写规则而不更改浏览器中的 URL

nginx - 配置 nginx 重写所有路径上的哈希值

php - 从 htaccess 上的 url 中删除/索引

php - Codeigniter 重定向 URL 问题

apache - mod_pagespeed 与亚马逊 S3

使用 mod_rewrite 的 JS/CSS 版本控制的 Apache 规则