V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
kechx
V2EX  ›  Apache

请教一条 .htaccess 规则

  •  
  •   kechx · Jan 21, 2016 · 2441 views
    This topic created in 3751 days ago, the information mentioned may be changed or developed.

    要求:将 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

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1169 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 18:14 · PVG 02:14 · LAX 11:14 · JFK 14:14
    ♥ Do have faith in what you're doing.