php - 使用 .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>
解决方案
ErrorDocument
是为此而生的。
ErrorDocument 404 /404.php
在 PHP 中使用$_SERVER['REDIRECT_URL']
来获取路径。
推荐阅读
- sql - 尝试在 aspnet_Membership 表上应用 Always Encrypted 加密时出错
- postgresql - 如何以不同的用户和不同的权限连接到 PostgreSQL
- r - 环境,R 中的 sys.frame() 和 sys.nframe
- typescript - 泛型函数的返回类型不能分配给实际返回类型
- github - 如何在 Jekyll GitHub Pages 上按页面分隔帖子?
- google-cloud-functions - 有没有办法将我的 Google Cloud Functions 支出分解为我的每个功能?
- php - SQL Server COUNT 字段不正确或语法错误
- javascript - 如何在 React 中使用 less.modifyVars?
- android - 我有什么方法可以从应用程序中更改全局通知声音吗?
- python - Maya python检查网格是否有动画