0

I want to echo on dynamic url but when I use ? then it is working and when I use / then the url changing but nothing echo out.

<?php
 $pages = array("story1", "story2", "story3", "story4");

 if(isset($_GET['stroy2'])) { 
      echo "You are reading Story 2!";
 }

 if(isset($_GET['stroy3'])) {
      echo "You are reading Story 3!";
 }
 ?>

<html>
<body>
  <form action="kids.php" method="post">

    // $i=2; here and below line is not working. This is the problem.
     <a href="http://www.abcd.com/kids.php/<?php echo $pages[$i];?>" <?php echo $pages[$i];?></a>
  </form> 
 </body>
 </html>

The line works when I use question mark "?" after kids.php? and the content is also changing:

<a href="http://www.abcd.com/kids.php?<?php echo $pages[$i];?>" <?php echo $pages[$i];?></a>

My problem is that I want to use / instead of question mark ?

5

1 Answer 1

3

You're looking for something that is called pretty URLs.

For example, if I have the page http://localhost/index.php?user=1 and want to turn it into http://localhost/users/1 you could add these lines to your .htaccess file.

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^user/(.*)$ ./index.php?user=$1

This stackoverflow question will most likely help you.

3
  • Thanks for the reply. Yes You are right and genius. I want to know there is not a number on the end its kids.php?story1 or kids.php?story2 .... how how to achieve this. Do I need to rewrite RewriteRule ^user/(.*)$ ./kids.php?story1, RewriteRule ^user/(.*)$ ./kids.php?story2, RewriteRule ^user/(.*)$ ./kids.php?story3 again and again.
    – Sarah
    Oct 3, 2018 at 14:19
  • @Sarah You should read up on regular expressions. In short, the RewriteRule above means: take whatever's inside the parenthesis (.*) and append it to the URL as a user parameter instead of the $1.
    – DanMan
    Oct 3, 2018 at 14:23
  • Look at the comment that @DanMan placed. He explains it very well. Also, please consider accepting the anwser so other people with similar problems can find a solution as well.
    – rpm192
    Oct 3, 2018 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.