首页 > 解决方案 > 删除网站 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

标签: phphtml

解决方案


推荐阅读