apache - 如何防止两个链接显示与.htaccess 相同的内容?

标签 apache .htaccess mod-rewrite seo rewrite

我有这个案例:

  • /css/<-- 文件夹
  • /css/something.css <-- 一个文件

如果用户访问文件夹 (/css/),我希望 Apache 返回该文件。如果用户试图直接访问文件 (/css/something.css),我希望他被重定向到该文件夹​​。

如何在没有循环的情况下执行此操作?我读了很多书,但我无法弄清楚应用两个规则,它总是返回一个循环。注意:这是我在 Stack Overflow 上的第一个问题。提前谢谢你。

最佳答案

将这些放在文档根目录中的 htaccess 文件中:

# internal rewrite to serve the file
RewriteRule ^css/$ /css/something.css [L]

# check if the actual request was for the file, then redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /css/something.css
RewriteRule ^css/something.css$ /css/ [R=301,L]

关于apache - 如何防止两个链接显示与.htaccess 相同的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11352727/

相关文章:

php - 权限被拒绝 :/var/www/abc/. htaccess pcfg_openfile : unable to check htaccess file, 确保它是可读的?

php - 我应该如何解决 facebook iframe 和浏览器安全性之间的冲突?

.htaccess - 重定向http=>https(非www)+ www https=>非www https(www 无证书)

.htaccess - 用一行 htaccess 让 URL 变得漂亮

php - 网站需要包含 index.php 的基本 url 才能正常工作

.htaccess - htaccess : %{HTTP_HOST} not interpreted in redirect destination

php - URL 重写 OpenCart 产品 SEO

php - 从子目录飞行 PHP 路由

wordpress - .htaccess/mod_rewrite.c : CakePHP application in a subfolder where the root folder hosts a Wordpress installation

c++ - 脚本头过早结束 : C++ cgi