【问题标题】:htaccess wasn't working on online server?htaccess 不能在在线服务器上工作?
【发布时间】: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


【解决方案1】:

如果您的 .htaccess 在本地工作,但不能在其他设备上工作,则可能是配置问题。

  1. 确保您在 php.ini 文件中启用了 mod_rewrite。修改 php.ini 后重启 apache。
  2. 还要确保将 apache 目录设置为 AllowOverride All。默认位置将包含在 &lt;Directory "/var/www/html"&gt; 之类的东西中,如果您更改此设置,请记住重新启动 apache。
  3. 如果您使用的是 iis 而不是 apache,则需要 web.config file instead

如果您想查看一个有效的 URL 缩短器,我几个月前构建了一个简单的 PHP,它带有 sqlite 后端和用户/登录管理。它使用类似于您的目标的干净 url 设置:https://github.com/danielson317/dphminify

【讨论】:

  • 感谢您回答我的问题,但这不是我的答案。我说我上面的 .htaccess 代码在 localhost 中运行良好,但在在线托管环境中却不行
  • 我想我当时不明白这个问题。如果 .htaccess 不起作用,您需要检查我的答案中提到的 apache 设置,并确保您为托管环境的操作系统使用正确的文件类型。例如如果宿主环境是 windows iis,你需要一个 web.config 来代替。如果 .htaccess 有效,您需要重新表述您的问题。
  • 不是答案。它提供了一些一般信息,但完全忽略了实际问题。
  • @arkascha 我不确定如何更具体。问题是“为什么我的 .htaccess 不起作用”。如果不直接访问相关服务器,我不确定如何更具体。如果您觉得我没有正确阅读该问题,您愿意为我澄清一下吗?
  • 你看,如果一个人不能回答一个问题,例如由于缺乏所需的信息,那么最好干脆回答这个问题,而是要求更多信息。这就是 cmets 的用途。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
相关资源
最近更新 更多