问题
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