Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 目录删除配置_Apache_.htaccess_Ubuntu - Fatal编程技术网

Apache 目录删除配置

Apache 目录删除配置,apache,.htaccess,ubuntu,Apache,.htaccess,Ubuntu,该网站是html,我有3.html文件,其中有3个同名目录。顺便说一句,Ubuntu18上有apache2 所以。。。。。。 file1.html /文件1 file2.html /文件2等等 如果不关闭.htaccess文件中的文件扩展名,则没有问题。但是,当这样做时,目录被提供,您会得到一个错误。下面是我的.htaccess配置试图使用DirectorySlash Off。在这个文件中还有一些其他的东西,但我想我会把它们都放在这里,以防有相关的因果关系发生 # BEGIN <IfMod

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

所以。。。。。。 file1.html /文件1 file2.html /文件2等等

如果不关闭.htaccess文件中的文件扩展名,则没有问题。但是,当这样做时,目录被提供,您会得到一个错误。下面是我的.htaccess配置试图使用DirectorySlash Off。在这个文件中还有一些其他的东西,但我想我会把它们都放在这里,以防有相关的因果关系发生

# 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
#开始
重新启动发动机
重写基/
选项+多视图
目录删除
#当文件和目录同名时重写为文件
重写cond%{DOCUMENT_ROOT}/$1.html-f
重写规则^([^.]+)$$1.html[L]
#允许加载不带扩展名的文件
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}\.html-f
重写规则^(.*)$$1.html[NC,L]
#删除文件扩展名
RewriteCond%{THE_REQUEST}/([^.]+)\.html[NC]
重写规则^/%1[NC,L,R]
RewriteCond%{REQUEST_FILENAME}.html-f
重写规则^%{REQUEST_URI}.html[NC,L]
从产品重定向到产品目录
重写规则^products/(.*)$/product/$1[R=301,NC,L]
#结束

当文件请求没有/与目录同名时,您可以为文件赋予优先级,还可以删除文件扩展名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]
然后添加您的rest规则:

RewriteRule ^products/(.*)$ /product/$1 [R=301,NC,L]
所以,通过上面的代码,HTML扩展名将被删除,然后服务器将首先检查是否有一个HTML文件匹配此请求,然后检查是否有一个目录匹配此请求

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

有关更多信息,您也可以参考我以前的回答: