【问题标题】:Remove .php from URL but it should still be pointing towards the original file (mod-rewrite)从 URL 中删除 .php 但它仍应指向原始文件(mod-rewrite)
【发布时间】:2021-03-03 10:46:41
【问题描述】:

亲爱的程序员们

我对 Web 开发还很陌生。尤其是 .htacces 文件。所以我正在开发一个网页,我想删除 .php 文件扩展名。如果我使用相同的代码,它适用于 html,但不适用于 php 扩展。

我想要实现的是当用户被重定向到https://website.com/pong.php 时,URL 会更改为:https://website.com/pong。但 URL 仍应指向第一个 URL。 HTML 文件也一样。

这就是我删除 html 扩展名的代码。有一行可以自动将页面重定向到 https 协议(第 3 行)

Options +MultiViews
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+)$ $1.html [NC,L]

我试过用这个来删除 php,但它没有用。这是我添加到代码中的行。

RewriteRule ^([^\.]+)$ $1.php [NC,L]

我试过这个问题的答案:removing .html or .PHP URL extension using .htaccess not working

但是当我尝试打开页面时它产生了这个错误:ERR_TOO_MANY_REDIRECTS

提前感谢您的帮助

【问题讨论】:

  • 您在这里尝试得很好(就新的和具有适用于 html 扩展文件的规则而言),就像您对 html 文件的规则一样(就逻辑而言)也适用于 php 扩展。现在的问题来了,你将如何分离哪些不存在的目录或文件应该重写为 PHP 文件,哪些要重写为 HTML 文件?如果您能在此处提及 Logic,我们肯定会在这里提供帮助,谢谢。
  • 我不完全理解你的意思。但我希望所有 .html 文件都在 URL 中删除 .html 并且对于 .php 文件也是如此
  • 好的,所以你的意思是你点击了这样的链接:http://localhost:80/test_singh.html 应该将 URL 更改为 http://localhost:80/test_singh(它应该只在后端由 test_singh.html 文件提供)并且与 php 文件相同?请确认一次好吗?

标签: apache .htaccess mod-rewrite


【解决方案1】:

对于您显示的示例,您能否尝试以下操作。 请确保在测试 URL 之前清除浏览器缓存。

RewriteEngine On
RewriteBase /

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

RewriteRule ^([^\.]+)$ $1.html [NC,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    相关资源
    最近更新 更多