V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
RihcardLu
V2EX  ›  Apache

请教一条 .htaccess 规则

  •  
  •   RihcardLu · 2016-01-21 17:22:02 +08:00 · 1918 次点击
    这是一个创建于 3020 天前的主题,其中的信息可能已经有所发展或是发生改变。

    要求:将 http://www.domain.com/a/xxx.html 所有 404 页面重定向到 http://www.domain.com/a/
    假设 http://www.domain.com/a/c.html 是一个正常页面,则正常显示;
    http://www.domain.com/a/test.html 返回 404 ,则重定向到 http://www.domain.com/a/

    之前写的规则

    RewriteEngine On
    RewriteBase /
    
    #redirect /a/xxxx to /a/
    RewriteCond %{REQUEST_URI} ^/a/
    ##don't rewrite if the resource already exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond {REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /a/ [R=301,L,NC]
    

    出现的问题的可能在于这两句

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond {REQUEST_FILENAME} !-d
    

    查了下资料发现这两句是将 URL 视为路径去服务器寻找相关目录 /文件,由于启用了路由规则,所以文件路径和 URL 不一样。

    Refer :
    1. http://www.cnblogs.com/adforce/archive/2012/11/23/2784664.html
    2. http://stackoverflow.com/questions/11906060/is-there-a-way-to-if-else-a-rewrite-in-apache-htaccess

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3206 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 12:18 · PVG 20:18 · LAX 05:18 · JFK 08:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.