How to prevent two links show the same content with .htaccess?

我的梦境 提交于 2019-12-24 07:37:38

问题


I have this case:

  • /css/ <-- a folder
  • /css/something.css <-- a file

If the user accesses the folder (/css/), I want that Apache returns the file. If the user tries to access the file directly, (/css/something.css), I want that he be redirected to the folder.

How can I do this without getting a loop? I've read a lot but I couldn't figure it out applying two rules, it always returns a loop. Note: this is my first question on Stack Overflow. Thank you in advance.


回答1:


Put these in your htaccess file in your document root:

# 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]


来源:https://stackoverflow.com/questions/11352727/how-to-prevent-two-links-show-the-same-content-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!