php - htaccess 不能在在线服务器上工作?
问题描述
我是 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]
索引.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 在本地工作但不在另一台设备上工作,则可能是配置问题。
- 确保在 php.ini 文件中启用了 mod_rewrite。更改 php.ini 后重新启动 apache。
- 还要确保您将 apache 目录设置为
AllowOverride All
.<Directory "/var/www/html">
如果更改此设置,默认位置将包含在记住重新启动 apache 之类的内容中。 - 如果您使用 iis 而不是 apache,则需要一个web.config 文件。
如果你想看到一个有效的 URL 缩短器,我几个月前用 sqlite 后端和用户/登录管理构建了一个简单的 PHP。它使用类似于您的目标的干净 url 设置:https ://github.com/danielson317/dphminify
推荐阅读
- c++ - 回调函数的可变参数模板实现
- tableau-desktop - Tableau 计算两个计算(countif 字段)的方差(除法)
- python - 完全匹配 QDoubleValidator 的解析器
- line - 单击按钮时重定向到调用应用程序
- excel - 高级过滤器在手动应用时有效,但在使用 VBA 应用时无效
- kivy - Kivy 应用程序在使用 pyinstaller 打包后无法执行。它说由于未处理的异常而无法执行脚本
- excel - VBA:pastespecial 始终粘贴为目标 Excel 表中的图像。尝试了所有可用的参数,但一切都粘贴图像而不是文本
- php - wordpress 插件问题 my_cred
- jmeter - 获取非 HTTP 响应消息:Taurus 容器上的连接重置消息
- android - 在 MaterialAlertDialogBuilder 中使用 Nagivation 时出现偶发性 java.lang.IllegalStateException