【问题标题】:Regex for .Htaccess Redirection.Htaccess 重定向的正则表达式
【发布时间】:2018-01-17 21:23:24
【问题描述】:

我构建了一个脚本来缩短长网址。一切正常,除了当我访问缩短的链接时,我得到了

错误"Not Found The requested URL /ESrdhPNLMV was not found on this server."

缩短的网址具有http://example.com/ESrdhPNLMV 格式。 “ESrdhPNLMV”部分作为标题存储在数据库中,作为长url的标题。

当前的 .htaccess 规则

RewriteEngine on
RewriteRule ^([^\/]+)\/?\/example.com\/?title=$1 [L,QSA]

我从某人那里得到了代码并用regex101.com 进行了修改,最终得到了上面的代码。 请问当我访问短链接时,重定向到数据库中存储的url的正确方法是什么。

下面是从db获取url的代码:

     if(isset($_GET['title'])){
      //select link from database
  $result = $db->prepare("SELECT * FROM links WHERE title =?");
  $result->bind_param("s",$_GET['title']);
  $result->execute();
  $goto = $result->get_result()->fetch_array();
  $g = $goto[1];
  header("Location: $g"); }

错误登录:

$goto = $result->get_result()->fetch_array();

即:

 PHP Fatal error:  Call to undefined method mysqli_stmt::get_result() in

读到上面的错误有一个 lin to mysqlnd 驱动程序,我检查了 phpinfo() 并发现驱动程序已启用。那我在这里做错了什么?

更新: 现在我已经能够解决错误消息了。然后去

http://example.com/?title=ESrdhPNLMV 

现在打开相同的http://example.com/?title=ESrdhPNLMV

【问题讨论】:

  • http://example.com/?title=ESrdhPNLMV 是否在浏览器中工作?
  • 不,我明白了; example.com 目前无法处理此请求。 HTTP 错误 500
  • 重命名您的 .htaccess 并重新测试此 URL
  • 同样的事情发生了,尝试了几种组合,都不好。这是否意味着问题来自我的代码?
  • 如果您在没有 .htaccess 的情况下遇到 500 错误,那么您需要检查 Apache error.log 以找出原因。

标签: regex .htaccess url-rewriting short-url


【解决方案1】:

您需要将规则更改为:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ /?title=$1 [L,QSA]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    相关资源
    最近更新 更多