skip to Main Content

Hi I had URLs like this…

www.example.com/list.php?type=vehicles&stype=Cars
www.example.com/list.php?type=vehicles&stype=Cars&loc=Ludhiana

And to sort results, sort variable was appending at last of URL and URL after adding sort variable was

www.example.com/list.php?type=vehicles&stype=Cars&sort='**ANYVALUE**'
www.example.com/list.php?type=vehicles&stype=Cars&loc=Ludhiana&sort='**ANYVALUE**'

And on href i was using

<a href="?<?php echo http_build_query(array_merge($_GET, array('sort' => 'sort-value'))); ?>"></a>

Above code was working perfectly for me. It was adding sort variable at end of URL as i want. Also it was modifying the value of sort variable if variable was existing already in URL.

But then i made my URLs SEO friendly and URLs now are

www.example.com/vehicles/Cars
www.example.com/vehicles/Cars/Ludhiana

After adding sort variable it becomes

www.example.com/vehicles/Cars&sort='**ANYVALUE**'
www.example.com/vehicles/Cars/Ludhiana&sort='**ANYVALUE**'

But now as URLs are SEO friendly, So when i use same href, it doesn’t work and it becomes

http://www.example.com/type=vehicle&stype=Car&sort=R-ASC

which results in a 404 error.

What will be the right href code so that it could do the following

  1. Append sort variable at the end of URL, whether the URL has 2 varibles or 3 variables.
  2. If sort variable already exists in URL, it will manipulate its value with current href.

Thanks in advance….

2

Answers


  1. The first parameters char should be an ?.

    http://www.example.com/?type=vehicle&stype=Car&sort=R-ASC
    
    Login or Signup to reply.
  2. I am not sure what do you need, but try something like that:
    Random HTML form that gets the attributes and their values (like ?type=vehicle&stype=blabla) and redirects you to the page you want (pure HTML used, without any PHP):

    <html>
    <head>
    <title>Test</title>
    </head>
    <body>
    <form action="http://example.com" method="get">
        <input type="text" placeholder="Type" name="type">
        <input type="text" placeholder="Stype" name="stype">
        <input type="submit" value="Search">
    </form>
    </body>
    </html>
    

    Also if you want a link to the page instead of getting redirect

    <head>
    <title>Test</title>
    </head>
    <body>
    <form action="" method="get">
        <input type="text" placeholder="Type" name="type">
        <input type="text" placeholder="Stype" name="stype">
        <input type="submit">
    </form>
    <?php
    echo "<a href='http://example.com/listItems.php?type=". $_GET["type"] ."&stype=". $_GET["stype"] ."'>Link</a>";
    ?>
    </body>
    </html>
    

    I hope i helped you less or more.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search