首页 > 解决方案 > 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";
            }


}



?>


标签: phpdatabase.htaccessmysqli

解决方案


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

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

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


推荐阅读