Redirect requests only if the file is not found?

后端 未结 6 1378
借酒劲吻你
借酒劲吻你 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 08:38
    # If requested resource exists as a file or directory, skip next two rules
    RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/$1 -d
    RewriteRule (.*) - [S=2]
    #
    # Requested resource does not exist, do rewrite if it exists in /archive
    RewriteCond %{DOCUMENT_ROOT}/archive/$1 -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/archive/$1 -d
    RewriteRule (.*) /archive/$1 [L]
    #
    # Else rewrite requests for non-existent resources to /index.php
    RewriteRule (.*) /index.php?q=$1 [L] 
    

    From Rewrite if files do not exist

    0 讨论(0)
  • 2020-11-28 08:42

    I think you want something along these lines:

    RewriteEngine on
    RewriteCond %{DOCUMENT_ROOT}clients/$1/$2 -f
    RewriteRule ^clients/([^/]+)/(.*)$ %{DOCUMENT_ROOT}clients/$1/$2 [L]
    RewriteRule ^clients/([^/]+)/(.*)$ %{DOCUMENT_ROOT}$2 [L]
    
    0 讨论(0)
  • 2020-11-28 08:54

    How about this?

    # If requested resource exists as a file or directory go to it
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule (.*) - [L]
    
    # Else rewrite requests for non-existent resources to /index.php
    RewriteRule (.*) /index.php?q=$1 [L]
    
    0 讨论(0)
  • 2020-11-28 08:55

    Try this:

    RewriteEngine on
    RewriteRule ^clients/abc/ - [L]
    RewriteCond %{DOCUMENT_ROOT}clients/abc/$0 -f
    RewriteRule .* clients/abc/$0 [L]
    
    0 讨论(0)
  • 2020-11-28 08:56
    RewriteCond %{REQUEST_FILENAME} !-f
    
    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题