两个正则表达式重写规则重定向404网址

时间:2011-07-18 09:52:52

标签: regex apache mod-rewrite redirect http-status-code-404

我有以下两个网址给了我一些讨厌的404错误,我想通过向apache添加一些重写规则来解决这个问题我必须认识到我不理解正则表达式足以能够写出正确的规则。

第一个网址

http://www.example.com/fincas-rusticas/finca-rustica_en_alacant-alicante/lalacanti/alicante/function.strtotime

必须在最后没有/function.strtotime的情况下重定向到同一个网址。

第二个网址,令人讨厌的网址。

  

http://www.example.com/casas/casas_en_malaga/malaga/malaga/    
严格标准:只应在 /var/www/vhosts/eldeposit.com/httpdocs/protected/helpers/Search.php 中通过引用传递变量 1398
/ casas / casas_en_malaga / malaga / malaga / venta-vivienda-29-473-24693-0-0

这个必须删除严格标准错误的部分。我不知道它是否可以用正则表达式完成,或者我最好在php中编程

2 个答案:

答案 0 :(得分:2)

1)使用此规则 - 它将从URL末尾删除function.strtotime(使用301永久重定向),无论该部分在URL中有多深(即它适用于example.com/something/function.strtotime as以及example.com/one/two/three/function.strtotime)。

RewriteEngine On
RewriteBase /

RewriteRule ^(.*)function\.strtotime $1 [L,R=301]

2)使用.htaccess无法修复此问题。这是PHP代码中的错误(听起来你直接传递了值而不是先将它分配给变量并传递变量),并且必须在PHP代码中修复。

答案 1 :(得分:0)

对于你可以做的第一个

RewriteRule fincas-rusticas/finca-rustica_en_alacant-alicante/lalacanti/alicante/function.strtotime fincas-rusticas/finca-rustica_en_alacant-alicante/lalacanti/alicante/ [L]

对于第二个,我必须假设此URL已经被重写为某些内容,因为严格的标准错误是PHP错误。这意味着你的地址是在某处处理的。

如果URL应该由脚本处理,它会被重定向到,我想你可以参考:Strict Standards: Only variables should be passed by reference

如果URL应该产生禁止错误,您可以执行

RewriteRule casas/casas_en_malaga/malaga/malaga/ - [F]