[英]remove trailing slash with htaccess
我想删除文件结尾(.html)和网址中的尾部斜杠。 当用户输入请求ttp://example.com/jobs/director.html时,他应被重定向到http://example.com/jobs/director 。 在大多数情况下,这是完美的方法,但是当页面名称等于子目录的名称时,尾部的斜杠不会被删除,服务器会尝试解析如下路径:/jobs/.html(在这种情况下,请求为http: //example.com/jobs/ )。 有人知道解决方案吗? 我真的被困住了。 谢谢!
我的文件结构:
作业(目录)
工作/ artist.html
工作/ director.html
的index.html
jobs.html
我的.htaccess文件:
Options +FollowSymLinks
RewriteEngine On
DirectorySlash Off
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
# remove .html file ending
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
您可以使用:
Options +FollowSymLinks
RewriteEngine On
DirectorySlash Off
# remove trailing slash to non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,L]
# add trailing slash internally to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?[^/])$ $1/ [L]
# add .html file ending internally
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.