2013-03-25 136 views
0

正如我以前的问题可能已经告诉你,我不是很好modrewrite。我试图搜索并为这个特定问题空洞起来。我希望有人能帮助我解决这个问题。冲突Modrewrite规则

我使用modrewrite作为短URL生成器的一部分,直接访问唯一ID会将它们转发到目标。添加/ v到结束将允许一个门户网站页面,将显示它们被转发到的URL(如预览页面)

因为/ v后面的ID,它工作得很好,但我想允许通过新增网页$ _GET

这是给予用户的快捷链接;

javascript:void(location.href='http://www.website.com/n/?url='+location.href) 

这是htaccess的

RewriteEngine On 

RewriteRule ^n actions.php?do=new [NS,L,NC] 
RewriteRule ^([a-zA-Z0-9]+)$ actions.php?id=$1 [NC] 
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ actions.php?id=$1&do=$2 [NC] 

的内容的问题: 因为/N是代替ID,它显然冲突。我已经尝试过NS和L,因为它们能够在规则匹配后停止执行。经过进一步检查,他们当然没有按照我的意愿工作。

最后,这里是一些如何在最终产品中寻找URL的例子;

New Submission: 
http://www.web.com/n/?url=http://www.google.com  [Outputs new URL as 12345] 
    Visit the ID directly: 
http://www.web.com/1A2b34D5      [Forwards to URL Destination] 
    Visit the ID Preview Link: 
http://www.web.com/1A2b34D5/v    [Displays preview, Delayed Forwarder] 

回答

1

如果你想从查询字符串中url变量只是添加QSA的选项,它会保存网址$_GET['url']

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f # if the url file does not exist 
RewriteRule ^n/?$ actions.php?do=new [NS,L,NC,QSA] 
RewriteRule ^([a-zA-Z0-9]+)$ actions.php?id=$1 [NC,L] 
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ actions.php?id=$1&do=$2 [NC,L] 

另外,在你的行动。PHP的,你必须把条件:

if (isset($_GET['do'])) 
{ 
    if ($_GET['do'] == 'new') // http://www.web.com/n/?url=http://www.google.com 
    { 
     if (!isset($_GET['url'])) die('No url'); 

     // save the url 
     // and outputs new URL 
    } 
    else if ($_GET['do'] == 'v') // http://www.web.com/1A2b34D5/v 
    { 
     // Visit the ID Preview Link 
    } 
} 
else // http://www.web.com/1A2b34D5 
{ 
    // Visit the ID directly 
} 

编辑:

我不知道它是怎么回事,但就像我在我的评论说,我在本地主机已经测试和它的作品,你预计,在我的测试中,我有这个内容var_dump($_GET);一个test.php即使我创建了一个actions.php文件具有相同的内容,这里是我测试过的一些URL示例:

例1:

http://localhost/n?url=google.comhttp://localhost/n/?url=google.com

此规则被执行:

RewriteRule ^n/?$ actions.php?do=new [NS,L,NC,QSA]

输出:

array(2) { ["do"]=> string(3) "new" ["url"]=> string(10) "google.com" }

例2:

http://localhost/n12345输出:array(1) { ["id"]=> string(6) "n12345" } http://localhost/nnnn456输出:array(1) { ["id"]=> string(6) "nnnn456" }

被执行此规则:

RewriteRule ^([a-zA-Z0-9]+)$ actions.php?id=$1 [NC,L]

示例3:

http://localhost/n12345/v输出:array(2) { ["id"]=> string(6) "n12345" ["do"]=> string(1) "v" }

此规则被执行:

RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ actions.php?id=$1&do=$2 [NC,L]

+0

提供将包括在混合品种下限,上限和数字值的字符串。这只是一个例子,目前确实有效。我对此模糊不清,并会相应调整。 – Tarquin 2013-03-25 02:20:07

+0

解答已更新! – tttony 2013-03-25 02:35:17

+0

非常感谢,但问题依然存在,它认为/ n是需要重定向的URL。示例(离线数据库)www.agn.im/n/?url=http://www.google.com/ – Tarquin 2013-03-25 03:34:22