【问题标题】:.htaccess with url shortener.htaccess 与 url 缩短器
【发布时间】:2021-04-24 01:10:24
【问题描述】:

我已经制作了一个网址缩短器应用程序,但似乎无法绕过 .htaccess 来确实缩短网址。 (我正在使用 xampp)

我的域名是 http://localhost/smd 我希望用户能够在 smd http://localhost/smd/code 之后添加一个 7 位代码 并将他们重定向到他们的页面 我当前的 .htaccess 代码是这样的:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_{7}]*$ redirect.php?shortened=$1  [L] 

将用户重定向到我的 smd 目录中的另一个页面,而不是正确的页面。 提前致谢!

我的重定向文件:

    <?php

include_once './class/Short.php';
date_default_timezone_set('Europe/Athens');

if(isset($_GET['shortened'])) {
    $short = new Short();
    $code = $_GET['shortened'];
    $short->shortened = $code;
    
    $short->getOneUrlByShort();
    
     $expire = $short->getExpiryFromShortened($code);
    
     $comp = (date('Y-m-d H:i:s')<=$expire['expiry_date']);
    $enabled = $short->is_enabled;

    if ($url = $short->getUrl($code) And $comp And $enabled == 1) {

        $redirect = $url['original'];
        header("Refresh: 3;".$redirect);
        die();
    }else if($url = $short->getUrl($code) And !$comp And $short->renewable == 1 And $enabled == 1){
        session_start();
        
        $short->renewable = 0;
        $newRenewVal = $short->renewable;
        
        $newExpiration = strtotime('+'.$short->active_period.' minutes');
        $formattedExpi = date('Y-m-d H:i:s', $newExpiration);
        
        $original = $url['original'];
        $_SESSION['original'] = $original;
        $_SESSION['expiration_date'] = $formattedExpi;
        

        $short->renewUrl($code, $formattedExpi, $newRenewVal);

       
        header('Location: http://localhost/smd/expiredAndRenew.php');
    }else {
        header('Location: http://localhost/smd/urlExpired.php');
    }

}



  ?>

我的当前目录看起来像1

【问题讨论】:

  • 欢迎来到 SO 并感谢您分享您的努力。您能否发布 URL 示例,例如-> 从哪个 url 到您要重写的哪个 url?只是示例网址应该没问题,让我们知道,它会让我们更好地理解您的问题,干杯。
  • redirect.php的内容是什么?因为那是实际重定向发生的地方。它是否根据缩短的参数从数据库中读取 URL,然后将其插入到header("Location: $url"); 中?如果不是,为什么不呢? (我也很确定您需要将 RegEx 中的方括号括在括号中才能实际获得 1 美元)
  • 在这种情况下,它并不是真正的 URL 缩短器(您可以在其中发布来自其他网站的任意长 URL),它只是简单的重写以获得对 SEO 友好的 URL。这意味着你可以忽略我的评论,除了最后一部分关于需要^([a-zA-Z0-9_{7}])*$
  • 请发布代码,而不是代码图像。你需要做一些基本的调试。使用 var_dump($_GET); die(); 之类的东西来检查 url 重写。

标签: .htaccess mod-rewrite url-rewriting seo friendly-url


【解决方案1】:

您是否可以使用您展示的示例尝试以下操作。请确保在测试 URL 之前清除浏览器缓存。此外,这只是在 uri 中抓取 smd(启用了忽略大小写)之后的所有内容,并将其作为查询字符串传递给您的 redirect.php。如果您在 smd 之后寻找特定的字符串/匹配文本,请在这部分更清楚地告诉我们。

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]{7})/?$ redirect.php?shortened=$1 [L]

【讨论】:

  • 它的工作非常感谢 SOOOOOOOOOOOOOOOOOOOOOOOOOO !!!!!!!!!!!!!!!!!! @RavinderSingh13 csan 你也添加了正则表达式?
  • @ΝίκοςΠαπαδάκος,真棒,很高兴在这里提供帮助:)
  • 它可以工作,但我需要它正好是 7 个字符
  • @ΝίκοςΠαπαδάκος,我想我现在感觉困了 :) 试试RewriteRule ^([a-zA-Z0-9-_]{7})/?$ redirect.php?shortened=$1 [L] 这个一次,让我知道?
  • 非常感谢,很抱歉打扰您,希望您度过一个愉快的夜晚!
猜你喜欢
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 2013-06-22
  • 2012-03-27
相关资源
最近更新 更多