Let .htaccess RewriteRule redirect to a script “in current dir” (instead of an explicit path)

前端 未结 1 576
你的背包
你的背包 2021-02-04 18:29

I\'m using a RewriteRule in .htaccess to redirect anything that is not an existing file, to a \"cms.php\" file which dynamically handles any request (or outputs some er

相关标签:
1条回答
  • 2021-02-04 18:44

    Typically, you'd just set a RewriteBase to the dev directory and leave the leading slash off of your rule's target:

    RewriteEngine On
    RewriteBase /subdir/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ cms.php [L,QSA]
    

    But if that's not good enough, and you want to be able to move the htaccess file around arbitrarily without having to alter the base all the time, ou can try to do something crazy by detecting an arbitrary base:

    RewriteCond %{ENV:URI} ^$
    RewriteRule ^(.*)$ - [ENV=URI:$1]
    
    RewriteCond %{ENV:BASE} ^$
    RewriteCond %{ENV:URI}::%{REQUEST_URI} ^(.*)::(.*?)\1$
    RewriteRule ^ - [ENV=BASE:%2]
    

    at the very top of your htaccess file, then using the BASE environtment variable:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ %{ENV:BASE}cms.php [L,QSA]
    
    0 讨论(0)
提交回复
热议问题