【问题标题】:DirectorySlash Off ConfigurationDirectorySlash 关闭配置
【发布时间】:2020-01-21 20:27:58
【问题描述】:

该网站是 html,我有 3 个 .html 文件,其中有 3 个同名目录。顺便说一句,它在 ubuntu 18 上的 apache2

所以...... 文件1.html /文件1 文件2.html /file2 以此类推。

如果您不关闭 .htaccess 文件中的文件扩展名,则没有问题。但是当这样做时,目录被提供并且你得到一个错误。所以这是我尝试使用 DirectorySlash Off 的 .htaccess 配置。此文件中还有其他一些内容,但我想我会保留所有内容,以防这里发生相关的因果关系。

# BEGIN
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options +MultiViews
DirectorySlash Off
# Rewrite to file when file and directory both exist with the same name
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^.]+)$ $1.html [L]
# Allows files to be loaded without extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
# Removes the file extensions
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Redirects from products to product directory
RewriteRule ^products/(.*)$ /product/$1 [R=301,NC,L]
</IfModule>
# END

【问题讨论】:

    标签: apache .htaccess ubuntu


    【解决方案1】:

    当请求没有 / 与目录同名以及删除文件扩展名 html 时,您可以优先考虑文件,如下所示:

    DirectorySlash Off
    RewriteEngine on 
    RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(html)[\s?/] [NC]
    RewriteRule ^ /%1%2 [R=302,L,NE]
    RewriteCond %{REQUEST_FILENAME}   !-f 
    RewriteCond %{REQUEST_FILENAME}\.html -f 
    RewriteRule ^(.*) /$1.html [L]
    RewriteCond %{REQUEST_FILENAME}   !-f 
    RewriteCond %{REQUEST_URI} !\/$
    RewriteRule ^(.*) %{REQUEST_URI}/ [L,R=302]
    

    然后添加你的休息规则:

    RewriteRule ^products/(.*)$ /product/$1 [R=301,NC,L]
    

    所以,通过上面的代码,HTML扩展将被删除,然后服务器将首先检查是否有一个HTML文件匹配这个请求,然后,如果有一个目录匹配这个请求。

    清除浏览器缓存然后测试,如果没问题,将302改为301以获得永久重定向

    更多信息,你也可以参考我之前的回答:

    Remove .php and .html extensions AND have directories with same name?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多