【问题标题】:htaccess how to redirect 404 to a variable url without causing infinite loop?htaccess 如何将 404 重定向到变量 url 而不会导致无限循环?
【发布时间】:2015-01-05 08:17:27
【问题描述】:

我有一个网站,当它在 url 的第一个相对路径上出现 404 错误时,我希望它立即重定向到与用户键入的内容相关的路径。

例如输入是:site.com/input 我希望这个第一级 url 字符串重定向到:site.com/m/input

我正在尝试这样的事情,它有效但会导致无限循环:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.php [L]
RewriteRule ^ http://www.website.com/m%{REQUEST_URI} [R,L]

所以基本上,我希望它只触发一次,如果 /m/{variable url} 的 url 不存在,那么我希望它登陆实际的 404 页面或 wordpress 默认的 RewriteRule 。 /index.php [L]

这可能吗? 非常感谢, 里昂

【问题讨论】:

  • 这正在重定向,但 404 页面不再显示,正确的页面也没有。两者都通过服务器而不是模板导致 404 页面。 RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !m RewriteRule ^(.*)$ website.com/m%{REQUEST_URI } [L,R=301]

标签: wordpress .htaccess mod-rewrite


【解决方案1】:

如果已经是/m/ URL,则添加另一个不重定向的条件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/m/
RewriteRule ^ http://www.website.com/m%{REQUEST_URI} [R,L]

【讨论】:

  • 干杯伙伴,我在 404.php 页面上使用了替代方案,似乎工作正常。 $url = 'http://' 。 $_SERVER['SERVER_NAME'] 。 $_SERVER['REQUEST_URI']; if (false !== strpos($url,'/m/')) { } else { header("HTTP/1.1 301 Moved Permanently"); header("位置:website.com/m/".$_SERVER['REQUEST_URI']); }
猜你喜欢
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-12
  • 1970-01-01
相关资源
最近更新 更多