从URL请求中删除.php文件扩展名 [英] Removing the .php file extension from the URL Request

查看:84
本文介绍了从URL请求中删除.php文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图美化我的URL,并希望摆脱.php文件扩展名.我在这里找到了其他几个问题,例如(删除PHP文件类型扩展),但所有建议都对我没有用.他们将我发送到我的404.php或根本不更改URL.

I am trying to beautify my URLs and want to get rid of the .php file extension. I've found a couple of other questions here such as this one (Removing the PHP file type extension) But none of the suggestions work for me. They send me to my 404.php or do not change the URL at all.

我认为htaccess中的整个RewriteEngine代码本身都存在冲突.

I figure the entire RewriteEngine code in my htaccess as a whole is conflicting in itself.

这是我到目前为止所得到的,

Here's what I got so far,

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


    RewriteBase /
    RewriteRule ^index\.php$ - [R]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /404.php [R]
</IfModule>

我希望我的域看起来像

https://www.example.com/about

推荐答案

请勿在同一.htaccess中混合使用RedirectRedirectMatchRewriteRule指令,因为它们来自不同的Apache模块,并且其加载顺序是不可预测的

Don't mix Redirect, RedirectMatch and RewriteRule directives in same .htaccess as they come from different Apache modules and their load sequence is unpredictable.

具有这种方式:

ErrorDocument 404 /404.php
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

在测试此更改之前,请确保清除浏览器缓存.

Make sure to clear your browser cache before testing this change.

这篇关于从URL请求中删除.php文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆