我已经做了一个url缩短应用程序,但似乎不能工作我的方式。htaccess确实使url更短。(我用的是xampp)
我的域名是http://localhost/smd我希望用户能够添加一个7位数的代码后SMD http://localhost/smd/code并将他们重定向到他们的页面我当前的。htaccess代码是这样的:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_{7}]*$ redirect.php?shortened=$1 [L]
将用户重定向到我的SMD目录中的另一个页面,而不是正确的页面。提前感谢!
my redirect file:
<?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
根据您所展示的样品,您可以尝试以下操作吗?请确保在测试url之前清除浏览器缓存。此外,这只是抓取smd
之后的所有内容(启用了ignorecase)在uri中,并将其作为查询字符串传递给您的redirect.php。如果您正在寻找smd之后的文本的特定字符串/匹配,那么请让我们更清楚地了解这部分。
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]{7})/?$ redirect.php?shortened=$1 [L]