【发布时间】:2019-06-20 05:37:43
【问题描述】:
我是 php 和 .htaccess 的新手,正在努力制作用户友好的 url-shotner。 例如:http://www.example.com/AS78654
我正在使用 .htaccess 来 $_GET index.php 上的链接变量。 它在本地主机上完美运行,但在上传到托管环境后却不是。当我点击 index.php 上的锚链接 (http://www.example.com/AS78654) 时,页面会自行刷新,让我知道我哪里出错了。 谢谢你
.htaccess
#Turn Rewrite Engine On
RewriteEngine On
# NC makes the rule non case sensitive
# L makes this the last rule that this specific condition will match
# $ in the regular expression makes the matching stop so that “customblah” will not work
# Rewrite for index.php?link=xxxxxxx
RewriteRule ^([0-9a-zA-Z]+)$ index.php?link=$1 [NC,L]
index.php
<form action="index.php" method="post">
<input type="text" name="long">
<input type="submit" name="submit">
</form>
<?php
// getting the unique code and redirect the visitor
if (isset($_GET['link']))
{
$con =mysqli_connect("localhost","useername","password","url");
$fetch = "SELECT * FROM shotner WHERE shot = '".$_GET['link']."' ";
$records = mysqli_query($con,$fetch);
while($row = mysqli_fetch_array($records))
{
$final_url = $row['longurl'];
header("location:".$final_url);
}
}
// inserting link & new codes into db
extract($_POST);
if(isset($submit))
{
$con = mysqli_connect("localhost","useername","password","url");
$shoturl = strtoupper(substr(md5(uniqid(mt_rand(0,9999))), 25));;
$query ="INSERT INTO shotner(longurl, shot) VALUES('".$long."','".$shoturl."')";
$res = mysqli_query($con, $query);
if($res)
{
echo '<a href=http://'."$_SERVER[HTTP_HOST]".'/url/'.$shoturl.'>http://'."$_SERVER[HTTP_HOST]".'/url/'.$shoturl.'</a>';
}
else
{
echo "problem with query";
}
}
?>
【问题讨论】:
-
您的
.htaccess文件是否被拾取?快速测试:打错字(例如RRewriteEngine on)。如果您收到 500 错误,则您的文件正在被拾取。如果没有任何变化,则忽略该文件。 -
是的。感谢您回答我的问题
-
你用的是什么http服务器?那个http主机的配置是什么?您在说什么“托管环境”?您需要提供更多详细信息,我们无法猜测您的情况。
标签: php database .htaccess mysqli