在URL

2011-08-01 18 views
0

我有一个看起来像这样的输入URL删除字符串的一部分查询字符串:在URL

http://localhost/20north/Numark/product/1/[email protected][email protected][email protected]! 

虽然重定向这对一个新的URL,我需要找到并删除所有出现“$ @!”从URL的最后一部分,使之成为:

http://localhost/20north/Numark/product/1/B004JPPO94 

注:最后一部分可以是任何东西,而不仅仅是[email protected][email protected][email protected]!。另外,[email protected]!的位置可以在最后部分的任何地方。

回答

1

如果你正在使用PHP,你可以做到以下几点:

<?php 
    $this_url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; 
    if(strpos($this_url, '[email protected]!') !== false) 
     die(header('Location: ' . str_replace('[email protected]!', '', $this_url))); 
?> 

编辑:更新的代码,使它变为动态

+0

谢谢,但我们没有使用php .... apache模块...我现在也添加到这些问题。无论如何感谢 –

3

使用mod_rewrite,你只需要这条规则:

RewriteRule ^(.*)\[email protected]!(.*)$ $1$2 [N] 

编辑:

其实,似乎有一个问题,当[email protected]!在URI的末尾。添加一个额外的规则,以删除尾随的匹配似乎修复它:

RewriteRule ^(.*)\[email protected]!$ $1 
RewriteRule ^(.*)\[email protected]!(.*)$ $1$2 [N] 

不知道为什么发生这种情况。

+0

好的单线! – ninetwozero

+0

漂亮的双线:-D! – Colin