【问题标题】:how to get from mod_rewrited url query string如何从 mod_rewrited url 查询字符串中获取
【发布时间】:2012-12-24 17:42:16
【问题描述】:

我有一个小的 mod_rewrite 问题。

  1. 我有 index.php(在根目录中),其中包含简单的 href 链接:

    <a href="novice/this-is-something">Novice</a>
    

当我点击 Novice 时,将我重定向到 novice.php 文件(它是 mod_rewrited,在 URL 中看起来不像 novice.php?query=this-is-something,但看起来像 novice/this-is-something (this-is-something 是我的查询))问题是,当我试图在 novice.php 文件中获取“this-is-something”查询时.

我在 novice.php 文件中得到这个查询:

    if (isset($_GET['query'])){
$query=$_GET['query'];

echo $query;
    }else{
echo 'Null parameters.';    
    }

但它只输出 0

但是当我在 href 链接中传递数字时,就像这样:

    <a href="novice/2131">Novice</a>

novice.php 文件中的输出正确:2131

我的 .htaccess 中有这样的代码:

    RewriteRule ^novice/([^/\.]+)/?$ novice.php?query=$1 [L]

那么我在 mod_rewrite 中做错了什么,我可以通过 $_GET 方法获取数字,但我无法通过 $_GET 获取字符串或字符?

【问题讨论】:

    标签: mod-rewrite get query-string


    【解决方案1】:

    我自己找到了解决办法。

    我刚刚更改了 RewriteRule 末尾的“查询”字 ^novice/([^/.]+)/?$ novice.php?query=$1 [L]

    “blabla”字 -> RewriteRule ^novice/([^/.]+)/?$ novice.php?blabla=$1 [L]

    然后一切正常……很奇怪……

        if (isset($_GET['blabla'])){
        $blabla=$_GET['blabla'];
    
       echo $blabla;
    }else{
     echo 'Null parameters.';    
    }
    

    它现在回显了参数this-is-something..但仍然不知道问题出在哪里..我刚刚更改了变量“query的名称strong>”,其中包含参数“this-is-something”..

    顺便说一句,如果有人感兴趣:我的 .htaccess 看起来像这样:

        Options +FollowSymLinks
        RewriteEngine On
    
        # Unless directory, remove trailing slash
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^([^/]+)/$ http://localhost/local_ageo.si/$1 [R=301,L]
    
        # Redirect external .php requests to extensionless url
        RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
        RewriteRule ^(.+)\.php$ http://localhost/local_ageo.si/$1 [R=301,L]
    
        # Resolve .php file for extensionless php urls
        RewriteRule ^([^/.]+)$ $1.php [L]
    
        RewriteRule ^novice/([^/\.]+)/?$ novice.php?blabla=$1 [L]
    

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      相关资源
      最近更新 更多