php - 删除网站 URL 中的文件扩展(.html 和 .php)和尾部斜杠
问题描述
我计划有一个代码能够同时在网站 URL 上删除文件扩展名.html、.php和尾部斜杠。我能怎么做?。
例如;
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
==================================================== =============
根据我两天前提出的问题,这就是我所做的,它与Web Server 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
解决方案
推荐阅读
- r - 如何将 as.character("%d-%m-%Y hh:mm") 转换为 "%d-%m-%y"
- java - 在“for”循环中执行的 Selenium 负测试用例
- laravel - Laravel SaveJson 到数据库
- flutter - DragTarget 不使用自定义 Draggable (Flutter) 调用 onWillAccept
- r - 在 R 中使用重新编码来更改观察名称
- javascript - 如何从 quill delta 对象制作描述文本
- java - 在扩展皮肤中调整 TextArea 的大小
- python - 哪些词汇术语描述了非函数变量的“回溯”分析代码?
- python-3.x - 如何检查一个点是否在python中的椭圆中
- bash - 将变量从多个 ssh awk 输出复制到本地文本文件