请教一条 .htaccess 规则

2016-01-21 17:22:02 +08:00
 RihcardLu

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

1936 次点击
所在节点    Apache
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/252424

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX