当'index.html'被剥离时,如何删除'/folder/index.html'的尾部斜杠?

时间:2012-11-05 15:27:03

标签: .htaccess mod-rewrite url-rewriting trailing-slash

我有一个具有以下文件/文件夹结构的静态网站:

  • 的index.html
  • / foobar的/
    • 的index.html
    • bob.html
    • alice.html

我想实现以下目标:

  • 删除所有.html个扩展名。 ✔有效
  • 删除index.html(分别为index)。 ✔有效
  • 我希望文件结尾而不用斜杠。 ✔有效
    • 如果有人添加了一个尾部斜杠,则重定向到不带斜杠的URL。 ✘不起作用
  • 我想要“文件夹”(文件夹中的index.html个文件)结束,而不是斜杠。 ✘不起作用
    • 如果有人添加了一个尾部斜杠,则重定向到不带斜杠的URL。 ✘不起作用

所以以下网址应该有效:

  • example.com/(实际上:/index.html
  • example.com/foobar(实际上:/foobar/index.html
  • example.com/foobar/bob(实际上:/foobar/bob.html
  • example.com/foobar/alice(实际上:/foobar/alice.html

以下请求应重定向(301):

  • example.com/foobar/重定向到:example.com/foobar
  • example.com/foobar/bob/重定向到:example.com/foobar/bob
  • example.com/foobar/alice/重定向到:example.com/foobar/alice

我发现当文件/foobar.html存在时会产生问题:当有人访问/foobar时,不清楚目录或文件是否被请求。但是,我会确保这种情况永远不会发生。


目前,我有.htaccess

# Turn MultiViews off. (MultiViews on causes /abc to go to /abc.ext.) 
Options +FollowSymLinks -MultiViews

# It stops DirectorySlash from being processed if mod_rewrite isn't. 
<IfModule mod_rewrite.c>

    # Disable mod_dir adding missing trailing slashes to directory requests.
    DirectorySlash Off

    RewriteEngine On

    # If it's a request to index(.html) 
    RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\  [NC]
    # Remove it. 
    RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]

    # Add missing trailing slashes to directories if a matching .html does not exist. 
    # If it's a request to a directory. 
    RewriteCond %{SCRIPT_FILENAME}/ -d
    # And a HTML file does not (!) exist.
    RewriteCond %{SCRIPT_FILENAME}.html !-f
    # And there is not trailing slash redirect to add it. 
    RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

    # Remove HTML extensions. 
    # If it's a request from a browser, not an internal request by Apache/mod_rewrite. 
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    # And the request has a HTML extension. Redirect to remove it. 
    RewriteRule ^(.+)\.html$ /$1 [R=301,L]

    # If the request exists with a .html extension. 
    RewriteCond %{SCRIPT_FILENAME}.html -f
    # And there is no trailing slash, rewrite to add the .html extension. 
    RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

</IfModule>

我需要在.htaccess更改/删除/添加哪些内容?我不太了解它。我试图删除块注释“如果匹配的.html不存在,添加丢失的尾随斜杠到目录”,但这没有帮助。

1 个答案:

答案 0 :(得分:4)

# Add missing trailing slashes to directories if a matching .html does not exist.规则正上方,尝试添加此规则,当存在html文件并且请求不是目录时重定向并且有一个尾部斜杠:

# if request has a trailing slash
RewriteCond %{REQUEST_URI} ^/(.*)/$
# but it isn't a directory
RewriteCond %{DOCUMENT_ROOT}/%1 !-d
# and if the trailing slash is removed and a .html appended to the end, it IS a file
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
# redirect without trailing slash
RewriteRule ^ /%1 [L,R=301]

这不应该与它后面的重定向规则冲突,因为它的条件检查完全相反。


编辑:

要处理index.html事物,您需要更改此规则,即附加尾部斜杠:

# Add missing trailing slashes to directories if a matching .html does not exist. 
# If it's a request to a directory. 
RewriteCond %{SCRIPT_FILENAME}/ -d
# And a HTML file does not (!) exist.
RewriteCond %{SCRIPT_FILENAME}.html !-f
# And there is not trailing slash redirect to add it. 
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

要:

# Add missing trailing slashes to directories if a matching .html does not exist. 
# If it's a request to a directory. 
RewriteCond %{REQUEST_FILENAME}/ -d
# And a HTML file does not (!) exist.
RewriteCond %{REQUEST_FILENAME}/index.html !-f
# And there is not trailing slash redirect to add it. 
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]    

这会在添加尾部斜杠之前检查目录中的index.html文件缺失。你必须的原因是因为information disclosure security issue when missing the trailing slash will actually expose all of your directory contents if you don't have the trailing slash。现在,添加这些规则以在index.html

时删除尾部斜杠
RewriteCond %{REQUEST_FILENAME} -d
# And a HTML file exists.
RewriteCond %{REQUEST_FILENAME}/index.html -f
# And there is a trailing slash redirect to remove it. 
RewriteRule ^(.*?)/$ /$1 [R=301,L]    

现在立即添加这些规则,以便在没有尾部斜杠时显式显示index.html(注意规则标志中没有R=301):

RewriteCond %{REQUEST_FILENAME} -d
# And a HTML file exists.
RewriteCond %{REQUEST_FILENAME}/index.html -f
# And there is no trailing slash show the index.html. 
RewriteRule [^/]$ %{REQUEST_URI}/index.html [L]