【问题标题】:How to prevent two links show the same content with .htaccess?如何防止两个链接使用 .htaccess 显示相同的内容?
【发布时间】:2012-07-05 21:19:24
【问题描述】:

我有这种情况:

  • /css/
  • /css/something.css

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

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

【问题讨论】:

    标签: apache .htaccess mod-rewrite seo rewrite


    【解决方案1】:

    将这些放在文档根目录下的 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]
    

    【讨论】:

    • 嘿@JonLin,感谢您的回复。对不起,我还没有接受你的回答,我无法尝试!一旦我尝试它,我就会回到这里并接受它(无论如何,看起来你是对的!:D也感谢您的解释)。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 2021-06-27
    • 2012-08-27
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多