如何在不创建新目录和将文件命名为index.php的情况下,从Web页面中删除文件类型。我想要将http://example.com/google.html变成http://example.com/google。
应该如何处理此问题。
PS:我尝试查看一些其他教程,但它们太复杂了。我知道可以在.htaccess中完成此操作。
如何在不创建新目录和将文件命名为index.php的情况下,从Web页面中删除文件类型。我想要将http://example.com/google.html变成http://example.com/google。
应该如何处理此问题。
PS:我尝试查看一些其他教程,但它们太复杂了。我知道可以在.htaccess中完成此操作。
是的,我知道这个问题已经被多次提出并得到了回答,但我会根据我的经验给出更全面的答案。
以下是一个.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 ^(.*)$ $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>
我想在这里强调一些重要的事情,供大家参考:
index.php
)。.php
扩展名,如果您还想删除其他扩展名(例如 .html
),请复制并粘贴第三个代码块,并将其中的 php
替换为其他扩展名。@armanP的以上接受答案并不会从PHP网址中删除
.php扩展名。它只是使得可以在结尾不使用.php
的情况下访问PHP文件。例如,/file.php
可以使用/file
或/file.php
进行访问,但这将导致您有两个指向同一位置的不同URL。
如果您想完全删除.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 ^(.*)/?$ /$1.php [L]
要删除.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 ^(.*)/?$ /$1.html [L]
针对 Apache 2.4* 用户的解决方案:
如果您的 Apache 版本是 2.4,您可以在不使用 RewriteConditions
的情况下使用代码。在 Apache 2.4 上,我们可以使用 END
标志代替 RewriteCond
来防止无限循环错误。
以下是适用于 Apache 2.4 用户的解决方案。
RewriteEngine on
#redirect /file.php to /file
RewriteRule ^(.+).php$ /$1 [L,R]
# now we will internally map /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [END]
注意:如果您添加了这些规则后,外部样式表或图像未加载,请使用以下方法修复:要解决此问题,您可以使链接变为绝对链接,将 <img src="foo.png>
改为 <img src="/foo.png>
。请注意,在文件名前加上 /
。或者在 Web 页面的头部添加以下内容将 URI 基本路径更改为 <base href="/">
。
您的网页无法加载 CSS 和 JS 的原因如下:
当浏览器 URL 从 /file.php
更改为 /file
时,服务器认为 /file
是一个目录,并试图在所有相对路径前添加它。例如:当您的 URL 是 http://example.com/file/
时,您的相对路径会更改为 <img src "/file/foo.png">
,因此该图像无法加载。您可以使用我在最后一段中提到的解决方案之一来解决此问题。
# Start of Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase
# From http:// to https://
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
# Redirect /file.php to /file with POST (enabled)
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
</IfModule>
# End of Apache Rewrite Rules
注意:
以上是接受没有.php的URL的代码,带有.php的URL将被转换为没有.php的形式(.php将被删除),所有文件/文件夹及其相应路径将正常打开,没有任何问题,同时post和get方法也能正常工作。
php
和html
扩展名,可以尝试访问这个链接:https://helponnet.com/2020/02/04/remove-html-and-php-extension-with-htaccess-rewriterule-url-rewriting-tips/。 - Amit Verma