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 .htaccess删除URL扩展名,添加尾部斜杠_Apache_.htaccess_Web - Fatal编程技术网

Apache .htaccess删除URL扩展名,添加尾部斜杠

Apache .htaccess删除URL扩展名,添加尾部斜杠,apache,.htaccess,web,Apache,.htaccess,Web,我一直在尝试让它在我正在开发的网站的客户端服务器上工作,但我就是无法让它工作。基本上,我试图删除.html扩展名,并添加一个尾随斜杠(在URL栏中) 因此,如果有人进来: -example.com/home/------------转到------example.com/home/ -example.com/home-----------转到------example.com/home/ -example.com/home.html------转到------example.com/home/

我一直在尝试让它在我正在开发的网站的客户端服务器上工作,但我就是无法让它工作。基本上,我试图删除.html扩展名,并添加一个尾随斜杠(在URL栏中)

因此,如果有人进来:

-example.com/home/------------转到------example.com/home/

-example.com/home-----------转到------example.com/home/

-example.com/home.html------转到------example.com/home/

-example.com/home.html/-----转到------example.com/home/

-example.com/home/.html------转到------example.com/home/

-example.com/home/.html/-----转到------example.com/home/

到目前为止,这是我的.htaccess,它工作得很好,并且做了我希望它做的一切,除了在末尾添加尾部斜杠

这是代码:

#force www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*) http://www.%{HTTP_HOST}/1 [R=301,L]

# remove .html ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.html\sHTTP/1)
RewriteRule ^(.+)\.html /1 [R=301,L,QSA]

# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/ /1 [L,R=301]

# rewrite to FILENAME.html if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) /1.html [L,QSA]
服务器上托管的所有我的文件都是FILENAME.html格式的,并且位于根目录中

因此,如果有人能帮助我,我将不胜感激。

修改.htaccess文件并插入以下内容 说明:

例如:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html 

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
更新页面上的链接
然后,所有超链接、css链接、图像等都需要更新,以具有绝对URL(
http://www.site.com/style.css
)或相对,并以
开头。
。否则,您将遇到诸如无法加载的CSS、无法工作的链接等问题。

不幸的是,我已经尝试过该解决方案,但它根本不适用于我。我刚刚得到一个错误404找不到页面。我在OP中发布的.htaccess工作得很好,只是没有在URL末尾添加斜杠。我已经更新了答案。试试这个,它更清晰一点。这对我也不管用。它添加了斜杠,但不加载任何CSS或图像。最重要的是,当我点击我网站上的任何链接时,它只会将它们添加到URL栏中的任何内容。假设我在example.com/home/上,然后我单击test,而不是example.com/test/进入example.com/home/test。您的链接、CSS、图像等都需要是绝对的或相对的,但以“./”开头。好的,我更改了您发布的代码的.htaccess,如果我输入的URL没有.html扩展名,它会添加一个斜杠,但如果我用.html扩展名键入它,它仍然会显示它。我希望.html扩展名自动消失,然后添加斜杠。