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

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

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

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

  • 我想实现以下目标:
  • 删除所有 .html扩展名。 ✔ 作品
  • 删除 index.html (分别为 index )。 ✔ 作品
  • 我希望文件结束时不带斜杠。 ✔ 作品
  • 如果有人添加了尾部斜杠,则重定向到没有尾部斜杠的 URL。 ✘ 不工作
  • 我希望“文件夹”(实际上是文件夹内的 index.html 文件)结束时不带斜杠。 ✘ 不工作
  • 如果有人添加了尾部斜杠,则重定向到没有尾部斜杠的 URL。 ✘ 不工作

  • 因此,以下 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 不存在,则向目录添加缺少的尾随斜杠”的块,但这没有帮助。

    最佳答案

    正上方您的 # 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]    
    

    关于.htaccess - 当 '/folder/index.html' 被剥离时,如何删除 'index.html' 的尾部斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13235180/

    相关文章:

    php - 使用 htaccess 将另一个域图像加载到我的域?

    .htaccess 通配符重定向修复

    regex - 设置 410 - 特定 url 的 STATUS GONE

    php - 是否可以只允许正确的漂亮网址?

    .htaccess - 产品页面的 Mod_rewrite 有效但类别页面无效

    regex - IIS 重写规则在实际环境中不起作用

    regex - htaccess 重写适用于所有人,并非有意

    PHP $_SESSION 有时工作有时不工作

    apache - 使用 HTACCESS 的 SEO 友好 URL

    .htaccess - 如何禁用单个文件夹的 CakePHP 重写路由,以便它可以用作第二个应用程序的位置?