问题描述
我计划拥有一个代码,可以同时删除文件扩展名.html、.PHP和尾部斜杠在网站 URL 上。我该怎么办?。
例如;
https://www.example.com/about/ to https://www.example.com/about
https://www.example.com/about.html to https://www.example.com/about
https://www.example.com/about.PHP to https://www.example.com/about
================================================ ================
从我在 2 天 前提出的问题来看,这就是我所做的,并且在 Web 服务器 Apache
中运行良好但是,如果您认为可以添加一些东西并使其如此美丽,那就更好了。
.htaccess 文件
DirectorySlash Off
RewriteEngine On
#=========== For HTML =====================
# If it's a request to index(.html)
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\ [NC]
# Remove it.
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]
# 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]
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]
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]
# 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]
#=========== For PHP =====================
# 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 .PHP appended to the end,it IS a file
RewriteCond %{DOCUMENT_ROOT}/%1.PHP -f
# redirect without trailing slash
RewriteRule ^ /%1 [L,R=301]
# Add missing trailing slashes to directories if a matching .PHP does not exist.
# If it's a request to a directory.
RewriteCond %{REQUEST_FILENAME}/ -d
# And a PHP file does not (!) exist.
RewriteCond %{REQUEST_FILENAME}/index.PHP !-f
# And there is not trailing slash redirect to add it.
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d
# And a PHP file exists.
RewriteCond %{REQUEST_FILENAME}/index.PHP -f
# And there is a trailing slash redirect to remove it.
RewriteRule ^(.*?)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -d
# And a PHP file exists.
RewriteCond %{REQUEST_FILENAME}/index.PHP -f
# And there is no trailing slash show the index.PHP.
RewriteRule [^/]$ %{REQUEST_URI}/index.PHP [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.PHP -f
RewriteRule ^(.*)$ $1.PHP
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)