.htaccess 与 url 缩短
.htaccess with url shortener
我制作了一个 url 缩短器应用程序,但似乎无法绕过 .htaccess 来缩短 urls。
(我正在使用 xampp)
我的域名是 http:///localhost/smd
我希望用户能够在 smd http://localhost/smd/code 之后添加一个 7 位代码
并将他们重定向到他们的页面
我当前的 .htaccess 代码是这样的:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_{7}]*$ redirect.php?shortened= [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
根据您展示的示例,您能否尝试以下操作。请确保在测试 URL 之前清除浏览器缓存。此外,这只是在 uri 中获取 smd
(启用忽略大小写)之后的所有内容,并将其作为查询字符串传递给您的 redirect.php。如果您正在寻找 smd 之后的特定 string/match 文本,请让我们更清楚地了解该部分。
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]{7})/?$ redirect.php?shortened= [L]
我制作了一个 url 缩短器应用程序,但似乎无法绕过 .htaccess 来缩短 urls。 (我正在使用 xampp)
我的域名是 http:///localhost/smd 我希望用户能够在 smd http://localhost/smd/code 之后添加一个 7 位代码 并将他们重定向到他们的页面 我当前的 .htaccess 代码是这样的:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_{7}]*$ redirect.php?shortened= [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
根据您展示的示例,您能否尝试以下操作。请确保在测试 URL 之前清除浏览器缓存。此外,这只是在 uri 中获取 smd
(启用忽略大小写)之后的所有内容,并将其作为查询字符串传递给您的 redirect.php。如果您正在寻找 smd 之后的特定 string/match 文本,请让我们更清楚地了解该部分。
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]{7})/?$ redirect.php?shortened= [L]