0

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


}



?>


  • 2
    Is your `.htaccess` file picked up at all? A quick test: make a typo (e.g. `RRewriteEngine on`). If you get a 500 error, your file is being picked up. If nothing changes, the file is ignored. – rickdenhaan Jan 26 '19 at 18:44
  • yes . thanks for answering my quetion –  Jan 26 '19 at 18:57
  • What http server are you using? And what is the configuration of that http host? What "hosting environment" are you talking about? You need to provide much more details, we cannot _guess_ your situation. – arkascha Jan 26 '19 at 19:41

1 Answers1

2

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

danielson317
  • 3,121
  • 3
  • 28
  • 43
  • thanks for answering my question but its not my answer. i said the .htaccess code i menshion above is working fine in localhost but not on online hosting environment –  Jan 26 '19 at 19:00
  • I guess I don't understand the question then. If .htaccess is not working you need to check the apache settings mentioned in my answer and make sure you are using the correct file type for the OS of the hosting environment. e.g. if the hosting environment is windows iis you need a web.config instead. If .htaccess is working you need to rephrase your question. – danielson317 Jan 26 '19 at 19:05
  • This is _not_ an answer. It offers some general information but completely ignores the actual question. – arkascha Jan 26 '19 at 19:41
  • @arkascha I'm not sure how to be more specific. The question is "Why is my .htaccess not working". Not sure how I can be more specific without direct access to the server in question. Would you care to clarify the question for me if you feel I did not read it correctly? – danielson317 Jan 26 '19 at 19:49
  • You see, if one cannot answer a question, for example due to lack of required information, then it is best simply _not_ to answer the question, but to ask for more information. That is what comments are for. – arkascha Jan 26 '19 at 19:55
  • @arkascha but I did provide a valid answer to the question. Just for the sake of clarity I have revised it to remove some of the distractions. Let me know if that is better. – danielson317 Jan 26 '19 at 19:57
  • I still do not consider this an answer, but only some general hints, since you do not point out what the actual issue is. But anyway, if you insist... – arkascha Jan 26 '19 at 20:07
  • Oh, and one more thing... please remove that advertisement at the end of your "answer"... – arkascha Jan 26 '19 at 20:09