.htaccess url-rewrite if file not exists

六眼飞鱼酱① 提交于 2019-11-26 22:56:15

问题


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?


回答1:


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



回答2:


It is not mentioned here but FallbackResource is the new recommended way of handling not-found (404) URLs. Example:

FallbackResource /not-404.php 

From Apache manual:

Use this to set a handler for any URL that doesn't map to anything in your filesystem, and would otherwise return HTTP 404 (Not Found).




回答3:


Implement a 404 error rule. Doesn't require mod_rewrite:

ErrorDocument 404 /default.php


来源:https://stackoverflow.com/questions/5469955/htaccess-url-rewrite-if-file-not-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!