user10972090
user10972090

Reputation:

htaccess wasn't working on online server?

I am new to php and .htaccess and working on making user-friendly url-shotner. example: http://www.example.com/AS78654

I am using .htaccess to $_GET the link variable on index.php. its perfectly working on localhost but after uploading on hosting environment it wasn't. the page refresh itself when i hit the anchor link(http://www.example.com/AS78654) on index.php let me know where i getting wrong. Thankyou

.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";
            }


}



?>


Upvotes: 0

Views: 1042

Answers (1)

danielson317
danielson317

Reputation: 3288

If your .htaccess is working locally but not on another device it is probably a configuration issue.

  1. Make sure you have mod_rewrite enabled in your php.ini file. Restart apache after making changes to php.ini.
  2. Also make sure you have you apache directory set to AllowOverride All. Default location would be wrapped in something like <Directory "/var/www/html"> Remember to restart apache if you change this setting.
  3. If you are using iis instead of apache you need a web.config file instead.

If you want to see a working URL shortener I built a simple PHP one a few months ago with sqlite backend and user/login management. It uses the clean url setup similar to what you are aiming for: https://github.com/danielson317/dphminify

Upvotes: 2

Related Questions