mod_rewrite - add path to REQUEST_FILENAME variabl

2020-07-27 05:14发布

问题:

Here is my code:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond application/public/%{REQUEST_FILENAME} -f 
RewriteRule (.*) application/public/$1 [L]

I want to make expression:

  • If file {REQUEST_FILENAME} doesn't exist AND if file application/public/%{REQUEST_FILENAME} exists do rewrite

Example results:

  • /style.css = /application/public/style.css
  • /gfx/logo.png = /application/public/gfx/logo.png
  • /index.php = /index.php
  • /Welcome = /index.php?input=Welcome (it's working)

What is proper code for this expression? I think i need to change line:

RewriteCond application/public/%{REQUEST_FILENAME} -f 

But i have no bloody idea how...

Full .htaccess listing:

RewriteEngine on

RewriteRule style,(.+).css tmp/merged/css,$1 [L,QSA]
RewriteRule script,(.+).js tmp/merged/js,$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond application/public/%{REQUEST_FILENAME} -f 
RewriteRule (.*) application/public/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule (.*) index.php?input=$1 [L,QSA]

(sorry for my english)

回答1:

i've found small workaround, but it's not elegant solution :(

RewriteEngine on

# Access to files in ./application/public
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule (.*) application/public/$1 [DPI]
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule application/public/(.*) $1 [DPI]

# Main rewrite
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule (.*) index.php?input=$1 [L,QSA,DPI]