【问题标题】:How to rewrite htaccess 404如何重写htaccess 404
【发布时间】:2018-09-15 14:39:00
【问题描述】:

我正在开发一个原始的 PHP Web 应用程序。但是,当我在 url 之后使用无效词(仅用于检查)时,我遇到了问题。

当前 htaccess 代码:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
ErrorDocument 404 https://example.com/404
Options -Indexes

例如:

我需要https://example.com/profile(没有扩展名)。现在正在使用以下 htaccess 代码。当我点击https://example.com/profile/lllflfllllflfl 是无效词,仅用于检查 404 错误)时,它会转到 404 错误页面。这也很好。但是,当我点击https://example.com/profile.php/lllflfl 时,我的网站仍然显示当前页面(当前页面表示https://example.com/profile 此页面)。我认为这需要重定向 404 页面。你的想法是什么?

谢谢。

【问题讨论】:

  • /file.php/foo/bar 指向 /file.php,因为 /foo/bar(文件名后的跟踪路径)是 PATH INFO 的一部分。默认情况下,它在 apache 上启用。您可以在 htacess AcceptPathInfo Off 中使用以下行禁用它。

标签: php .htaccess http-status-code-404


【解决方案1】:
RewriteRule ^profile.php/.*$ /404pagehere.php [L]

【讨论】:

  • 现在 profile.php 从名为 admin 的子文件夹加载。我当前的网址是example.com/admin/profileprofile.php 也是一个示例页面。我有太多这样的页面(例如:仪表板、add_user、user_list 等)。 400.php 位于根文件夹中。那么这个RewriteRule是什么?
  • 在 ^ 符号后调整我们给您的所有内容以拥有 admin/
  • 这不起作用。我仍然可以看到在使用无效的 url 结束后损坏。我点击prntscr.com/j1dosh 查看我当前的文件列表。目前我使用RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^.]+)$ $1.php [NC,L] RewriteRule ^admin/*$ 404.php ErrorDocument 404 https://st.dev.krazyit.com.au/404 Options -Indexes
【解决方案2】:

如果您不希望 @starkeen 的建议非常重要,您可以这样做:

RewriteRule ^(.*)\.(php|html|htm)/.*$ - [L,R=404]

并让它们被视为错误请求。

【讨论】:

  • 美丽。谢谢你。 :)
猜你喜欢
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 2013-08-03
  • 2023-04-08
相关资源
最近更新 更多