删除的.html和.PHP与htaccess的扩展html、PHP、htaccess

2023-09-02 09:49:09 作者:想变得更好然后去拥抱你

我如何删除我的网页文件类型,而无需创建一个新的目录和命名index.php文件。我想 http://example.com/google.html 到的 http://example.com/google 。

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.html to http://example.com/google.

我怎么会去这样做。

PS:我试图寻找一些其他的教程,但那里混乱。我现在做的,它可以在.htaccess做

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

推荐答案

是的,我知道这个问题是问多遍了,并回答了,但我会根据我的经验给多一点COM prehensive答案

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.

下面是的.htaccess code段,将帮助你:

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

# 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 ^(.*)$ $1/ [R=301,L]

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

# End of Apache Rewrite Rules
 </IfModule>

我要强调一些重要的东西在这里供大家参考:

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

这code段不会从URL中删除条目脚本(如 的index.php 被许多PHP框架) 它只是删除的.php 扩展,如果你想删除其它分机以及(如的.html ),复制并粘贴3号地块和替换 PHP 与其他扩展。 不要忘了还可以删除扩展名由锚(链接)HREF。 This code snippet doesn't remove entry scripts from url (such as index.php used by many PHP frameworks) It only removes .php extension, if you want to remove other extension as well (e.g. .html), copy and paste 3rd block and replace php with other extension. Don't forget to also remove extension from anchors (links) href.