简体   繁体   English

使用.htaccess删除.php和.html

[英]Removing .php and .html with .htaccess

I am attempting to remove .html and .php extensions from my website's URLs. 我正在尝试从网站的网址中删除.html和.php扩展名。 I have been suggested to use the following code: 建议使用以下代码:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

This indeed works, however it will only work for one extension at a time. 这确实有效,但是一次仅可用于一个扩展。 So I can either have .html removed, or .php removed from the URL. 因此,我可以从网址中删除.html或.php。 I need to have both removed simultaneously. 我需要同时删除两者。 Someone suggested I just rename all my .html files to .php, however this will not work for me. 有人建议我将所有.html文件重命名为.php,但这对我不起作用。 Previously, I was able to remove both extensions purely using .htaccess, but am unable to find the solution that I used before. 以前,我能够仅使用.htaccess删除这两个扩展名,但是找不到以前使用的解决方案。

I also tried this code and it only removes the extension for .html: 我也尝试了这段代码,它只删除了.html扩展名:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

What code do I need to make this work? 我需要什么代码才能完成这项工作?

Thank you in advance for your assistance! 预先感谢您的协助!

if php files have more prioritet, than html ones, swap groups of lines 如果php文件比html文件具有更多优先级,则交换行

RewriteEngine On
# if exist file.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# if exist file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM