【问题标题】:htaccess redirect all 404 to index.php with parameters but don't change address barhtaccess 使用参数将所有 404 重定向到 index.php 但不更改地址栏
【发布时间】:2016-04-23 20:18:01
【问题描述】:

我要做的是将所有不存在的 URL 重定向到 index.php?q=%URL%,例如:
www.example.com/ggg 将重定向到 www.example.com/index.php?q=ggg

但是,我希望地址栏仍然显示 www.example.com/ggg

到目前为止我得到的是:

Options +FollowSymLinks
RewriteEngine On

# redirect all requests to non-existing resources to special handler
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/? /index.php?q=$1 [L,NC,R]

但它一直在更改地址栏,而且我不确定它是否一直有效。

有什么建议吗? 谢谢

【问题讨论】:

  • 实际上我想重定向所有错误页面,而不仅仅是 404。谢谢

标签: apache .htaccess mod-rewrite redirect url-rewriting


【解决方案1】:

如果您想将错误页面重写为 index.php,请删除 R 标志。

Options +FollowSymLinks
RewriteEngine On

# redirect all requests to non-existing resources to special handler
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?q=$1 [L,NC]

【讨论】:

  • 遇到了另一个问题.. 当我转到 example.com/ggg/ddd 时,它会重定向到 index.php?q=ggg 而没有 /ddd 部分...我怎样才能重定向整个事情? (我希望“example.com/”之后的所有内容都是我的参数)
  • @User3720583 查看编辑,我将正则表达式模式从 ^([^/]+)/?到 ^(.+)$ 它匹配任何请求。
猜你喜欢
  • 2010-11-21
  • 2013-08-26
  • 2013-06-11
  • 1970-01-01
  • 2013-09-09
  • 2015-04-10
  • 2012-10-28
  • 1970-01-01
  • 2016-11-30
相关资源
最近更新 更多