【问题标题】:Apache Redirects - .htaccess - ReWriteCondApache 重定向 - .htaccess - ReWriteCond
【发布时间】:2013-07-17 19:28:35
【问题描述】:

我正在尝试操纵我的博客服务器上的 .htaccess 文件,以允许重定向某些内容,同时允许访问其他内容。

我想做的是允许访问任何扩展名为(gif|jpg|css|js|ico|png) 的文件以及一个目录/includes/ 和一个文件sitemap.html,而我想发送到站点地图的所有其他流量。 html文件。

这可能吗?

这是我尝试使用的代码,但它似乎没有按预期工作。重定向匹配文件扩展名并允许访问 sitemap.html 文件,但无论实际目录中有哪些文件,我希望从用户角度允许访问的 /includes/ 目录似乎仍然重定向到站点地图.html 文件。

RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.(gif|jpg|css|js|ico|png)$|^/includes$|^/sitemap\.html$)
RewriteRule ^(.*)$ /sitemap.html [R=301,L]

任何帮助将不胜感激。

【问题讨论】:

  • 从^/includes$ 中删除$,因为它会阻止匹配除裸/includes 之外的任何内容

标签: apache .htaccess redirect mod-rewrite


【解决方案1】:

试试这个:

RewriteCond %{REQUEST_URI} !\.(gif|jpg|css|js|ico|png)$ [NC]
RewriteCond %{REQUEST_URI} !^/(sitemap\.html|includes(/.*|))$ [NC]
RewriteRule ^ /sitemap.html [R=301,L]

当然,两个 RewriteCond 行都可以合并为 1,但我建议将其 2 分开,因为这样更易于阅读且易于维护,以防您将来要添加新的扩展或新目录以进行排除。

【讨论】:

  • RewriteRule ^ /sitemap.html [R=301,L] 是做什么的?
  • 如果上述2个条件成功,则将当前URL转发到/sitemap.html。
猜你喜欢
  • 2013-09-23
  • 2015-05-30
  • 2011-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多