Redirect requests only if the file is not found?

后端 未结 6 1392
借酒劲吻你
借酒劲吻你 2020-11-28 08:23

I\'m hoping there is a way to do this with mod_rewrite and Apache, but maybe there is another way to consider too.

On my site, I have directories set up for re-skinn

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 09:05

    I seemed to have at least one problem with each of the examples above. %{DOCUMENT_ROOT} seemed to do the wrong thing in certain places, and some / characters seem to be missing. Here is my solution, which goes in the .htaccess in the web root.

    Instead of using two rules (one for the case where the file under clients/ is found, and one for not found), all I need to check is if the requested file (or directory) does NOT exist. Because if it exists, no change is needed, it can just use the file provided in the client dir. Here's the code I settled on:

    RewriteEngine on
    RewriteCond %{DOCUMENT_ROOT}/clients/$1/$2 !-f
    RewriteCond %{DOCUMENT_ROOT}/clients/$1/$2 !-d
    RewriteRule ^clients/([^/]+)/(.*)$ $2 [L]
    

    Thanks for your help!

提交回复
热议问题