首页 > 解决方案 > 使用 .htaccess 和 php 代码在 seo 友好的 .htaccess 中处理不存在的页面

问题描述

我在 url 中具有以下.htaccess力量https和力量www,但我也想用我的 404 php 页面处理不存在的页面。

例如下面的代码适用于任何现有页面,但如果页面不存在,将显示 404 页面。

在这种情况下,我希望我的自定义 404 页面代码与整个uri/query strings/parameters不存在的页面一起处理它。

因此,如果https://www.example.com/w?id=10调用它,如果它不存在,它将重定向到带有所有查询字符串参数的 404 页面(在本例中:id=10)

DirectoryIndex home.php
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
</IfModule>
<Files ~ "\.sql$">
  Order allow,deny
  Deny from all
</Files>
<Files ~ "\.zip$">
  Order allow,deny
  Deny from all
</Files>
<Files .htaccess>
order allow,deny
deny from all
</Files>

标签: php.htaccesshttp-status-code-404

解决方案


ErrorDocument是为此而生的。

ErrorDocument 404 /404.php

在 PHP 中使用$_SERVER['REDIRECT_URL']来获取路径。


推荐阅读