的.htaccess URL重写,如果不存在的文件 [英] .htaccess url-rewrite if file not exists

查看:503
本文介绍了的.htaccess URL重写,如果不存在的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做小动作的网站! 我们的想法是:

  • 如果一个所需的URL的文件存在,那么我去那个网址,什么都不做多;
  • 如果不存在所需的URL的文件,我一定要去file.php,位于比做一些事情,但不改变网址!

例如:

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

www.mysite.com/page2.htm - >不存在 - >转到文件default.php,但与URLwww.mysite.com/page2.htm

这是可能所有的.htaccess做到这一点?

解决方案

  RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^ * $ /default.php [L]
 

I must do a little trick for a site! The idea is:

  • if a file for a required url exists then I go to that url, doing nothing more;
  • if a file for a required url not exists, I must go to a file.php and than do something, but NOT changing the url!

example:

www.mysite.com/page1.htm -> exists -> go to file page1.htm

www.mysite.com/page2.htm -> NOT exists -> go to file default.php but with url "www.mysite.com/page2.htm"

It's possible to do this all by .htaccess?

解决方案

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

这篇关于的.htaccess URL重写,如果不存在的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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