冲突的Modrewrite规则


Conflicting Modrewrite Rules

正如我之前的问题可能已经告诉你的那样,我对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]

如果您想从查询字符串中获得url变量,只需将QSA添加到选项中,它将保存url在$_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]

同样,在actions.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
}
编辑:

我不知道它是怎么回事,但就像我在我的评论中说的,我已经在我的本地主机上测试过了,它像你期望的那样工作,在我的测试中,我有一个test.php与这个内容var_dump($_GET);,甚至我已经创建了一个actions.php文件与相同的内容,这里是一些url的例子,我已经测试:

例二:

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" }

Example2:

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]

青年们:

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]