【发布时间】: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