使用 .htaccess 删除 .html 和 .php 扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21617506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:51:06  来源:igfitidea点击:

Remove .html and .php extensions with .htaccess

html.htaccessmod-rewriterewrite

提问by SimplePi

How do I remove the file type from my webpages without creating a new directory and naming the file index.php. I want http://example.com/google.htmlto http://example.com/google.

如何在不创建新目录和命名文件 index.php 的情况下从我的网页中删除文件类型。我想要http://example.com/google.htmlhttp://example.com/google

How would I go about doing this.

我将如何去做这件事。

PS: I tried looking at some other tutorials but there to confusing. I do now that it can be done in .htaccess

PS:我尝试查看其他一些教程,但令人困惑。我现在可以在 .htaccess 中完成

回答by Arman P.

Yes, I know that this question was asked multiple times already and is answered, but I will give a little more comprehensive answer based on my experience.

是的,我知道这个问题已经被问过多次并得到了回答,但我会根据我的经验给出更全面的答案。

Here is the .htaccess code snippet that will help you:

这是 .htaccess 代码片段,可以帮助您:

# Apache Rewrite Rules
 <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ / [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ .php 

# End of Apache Rewrite Rules
 </IfModule>

I want to stress some important things here for everybody's reference:

在这里我想强调一些重要的事情,供大家参考:

  • This code snippet doesn't remove entry scripts from url (such as index.phpused by many PHP frameworks)
  • It only removes .phpextension, if you want to remove other extension as well (e.g. .html), copy and paste 3rd block and replace phpwith other extension.
  • Don't forget to also remove extension from anchors (links) href.
  • 此代码片段不会从 url 中删除入口脚本(例如 index.php许多 PHP 框架使用的)
  • 它只删除.php扩展名,如果您还想删除其他扩展名(例如.html),请复制并粘贴第三个块并替换php为其他扩展名。
  • 不要忘记从锚点(链接)href 中删除扩展名。

回答by starkeen

@armanP's accepted answer above does not remove.php extension from php urls. It just makes it possible to access php files without using .phpat the end. For example /file.phpcan be accessed using /fileor /file.phpbut this way you have 2 diffrent urls pointing to the same location.

@armanP 上面接受的答案不是remove来自 php url 的 .php 扩展名。它只是使访问 php 文件成为可能,而无需.php在最后使用。例如,/file.php可以使用/file或访问,/file.php但是这样您就有 2 个不同的 url 指向同一位置。

If you want to remove .phpcompletely, you can use the following rules in /.htaccess:

如果要.php完全删除,可以在 中使用以下规则/.htaccess

RewriteEngine on 
#redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
# now we will internally map /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /.php  [L]

To remove .html,use this

要删除.html,请使用此

 RewriteEngine on
 #redirect /file.html to /file
 RewriteCond %{THE_REQUEST} \s/([^.]+)\.html [NC]
 RewriteRule ^ /%1 [NE,L,R]
 # now we will internally map /file to/ file.html
 RewriteCond %{REQUEST_FILENAME}.html -f
 RewriteRule ^(.*)/?$ /.html  [L]

Solution for Apache 2.4* users :

Apache 2.4* 用户的解决方案:

If your apache version is 2.4 ,you can use the Code without RewriteConditionsOn Apache 2.4 we can use ENDflag instead of the RewriteCondto prevent Infinite loop error.

如果你的 apache 版本是 2.4,你可以使用没有RewriteConditions在 Apache 2.4 上的代码,我们可以使用END标志而不是RewriteCond来防止无限循环错误。

Here is a solution for Apache 2.4 users

这是 Apache 2.4 用户的解决方案

 RewriteEngine on

 #redirect  /file.php to /file
  RewriteRule ^(.+).php$ / [L,R]
 # now we will internally map /file to /file.php
 RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /.php [END]

Note : If your external stylesheet or images aren't loading after adding these rules ,to fix this you can either make your links absolute changing <img src="foo.png>to <img src="/foo.png>.Notice the /before the filename . or change the URI base add the following to head section of your Web page <base href="/">.

注意:如果在添加这些规则后您的外部样式表或图像没有加载,要解决此问题,您可以将链接绝对更改<img src="foo.png>为 。<img src="/foo.png>注意/文件名之前。或更改 URI 基础,将以下内容添加到网页的头部部分<base href="/">

Your Webpage fails to load css and js due to the following reason :

由于以下原因,您的网页无法加载 css 和 js:

When your browser url changes from /file.phpto /fileserver thinks that /fileis a directory and it tries to append that in front of all relative paths . For example : when your url is http://example.com/file/your relative path changes to <img src "/file/foo.png">thus the image fails to load . You can use one of the solutions I mentioned in the last peragraph to solve this issue.

当您的浏览器 url 更改/file.php/file服务器时/file,它认为这是一个目录,并尝试将其附加到所有相对路径的前面。例如:当您的 url 是http://example.com/file/您的相对路径时<img src "/file/foo.png">,图像无法加载。您可以使用我在上一段中提到的解决方案之一来解决此问题。