plqws
V2EX  ›  Apache

请教一个 Apache 的 RewriteRule 问题

  •  
  •   plqws · Dec 19, 2015 · 2167 views
    This topic created in 3834 days ago, the information mentioned may be changed or developed.
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/cache/
    RewriteRule ^(.*)$ /cache/$1
    #我想要如果匹配到 /cache/$1 的话就此结束
    #若没有匹配到 /cache/$1 的话就执行下面的那个 index.php
    RewriteRule ^ index.php [QSA,L]
    

    目录结构
    /root
    -cache
    |-test.txt
    -index.php
    -.htaccess

    比如访问 /test.txt ,就能直接访问到 /cache/test.txt ,若访问 /hello.txt 就重写成 /index.php?blahblahblah

    该如何实现呢?

    Supplement 1  ·  Dec 19, 2015

    最后还是自己解决了

    RewriteEngine On
    
    RewriteRule !^cache/ cache%{REQUEST_URI}
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^ index.php [QSA,L]
    

    参考: http://stackoverflow.com/questions/1165341/apache-rewriterule-to-subdirectory

    1 replies    2015-12-19 14:07:38 +08:00
    Slienc7
        1
    Slienc7  
       Dec 19, 2015
    [L]
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2717 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 03:02 · PVG 11:02 · LAX 20:02 · JFK 23:02
    ♥ Do have faith in what you're doing.