1

What's the best way to remove the = and ? in a URL from the get method in PHP? I'm working on the pagination structure of my website and currently this is what the URLs look like:

www.example/test/?page=3

I want it to look like this:

www.example/test/3

Can this be addressed directly in the PHP get method with some extra code or does it have to be done through an htaccess file?

This is what my htaccess file looks like right now:

RewriteBase /  
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]
Anwar
  • 4,162
  • 4
  • 41
  • 62
Mason Peace
  • 93
  • 1
  • 10
  • 1
    Do it in php. It is cleaner and better than doing a redirect everytime the url matches the rule – Cornel Raiu Jul 05 '17 at 17:15
  • Can you please point me to a tutorial or something that teaches how to accomplish this? – Mason Peace Jul 05 '17 at 17:20
  • If you insist in using PHP instead of an htaccess, you could use `array_values()` to delete the `page=3` or any other query strings. – Anwar Jul 05 '17 at 17:21
  • Add the php code that is generating the pagination links and show us what you tried to get it working and we will show you what you need to do or point you to similar questions or other resources – Cornel Raiu Jul 05 '17 at 17:22
  • This is the PHP code that is generating the pagination links. https://stackoverflow.com/questions/44916337/php-count-all-elements-in-an-array – Mason Peace Jul 05 '17 at 17:42
  • 1
    There are two parts on the URL beatification: in the PHP code you generate them (`www.example/test/3` f.e.) and in the `.htaccess` you rewrite them to regular URLs (`www.example/test/?page=3`). Then you get in `$_GET['page']` the value `3`. There is no need to do URL parsing in the PHP code, the interpreter handles it. – axiac Jul 05 '17 at 22:39
  • I assume the URL `www.example/test/?page=3` actually requests the `index.html` (directory index) document with the `/test` subdirectory, ie. `www.example/test/index.html?page=3`? – MrWhite Jul 06 '17 at 00:05
  • @user82217 yes, that is correct. – Mason Peace Jul 06 '17 at 03:14
  • @axiac I'm confused, you said "there is no need to do URL parsing in the PHP code". What does that mean? So how would I prevent the GET method from adding a ? and = – Mason Peace Jul 06 '17 at 03:16

2 Answers2

1

Here is my try, I compacted some already existing answer from different StackOverflow topics dealing with URL extraction and I came up with this solution :

<?php
    function http_protocol() {
        /**
         * @see https://stackoverflow.com/a/6768831/3753055
         */
        return (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://';
    }

    function http_host() {
        return $_SERVER['HTTP_HOST'];
    }

    function http_uri() {
        return parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
    }

    function http_refactored_query_strings() {
        $queries = explode('&', $_SERVER['QUERY_STRING']);
        $refactoredQueries = [];

        foreach( $queries as $query ) {
            $refactoredQueries[] = filter_var(explode('=', $query)[1], FILTER_SANITIZE_STRING);
        }

        $queries = implode('/', $refactoredQueries);

        return $queries ?: '';
    }

    function http_refactored_url() {
        return http_protocol() . http_host() . http_uri() . http_refactored_query_strings();
    }

    echo http_refactored_url();
?>

Tryied with some examples :

For the query string refactoring part, I used $_SERVER['QUERY_STRING] and exploded the value to & character. because the first ? is not contained in $_SERVER['QUERY_STRING']. So you come up with lot of arrays, containing strings like page=3, view=page, ... And foreach one, you split it using = delimiter and get the second element (index : 1) to append it to the solution.

Hope it helps

Anwar
  • 4,162
  • 4
  • 41
  • 62
  • Thanks for this but there's a tiny issue. It's adding an extra '/'. This is how the url looks like www.example.com/test//3 Can you please update your code to remove the extra ' / ' ? – Mason Peace Jul 06 '17 at 03:41
  • 1
    Removed unecessary function and improved security using `filter_var` – Anwar Jul 06 '17 at 08:39
  • Thank you so much for this. I just have one last question, this is the PHP code that is generating the URL in my website. When I place your code in my PHP code, it doesn't fix the URL but instead it adds the correct URL as a string in the UI. What am I missing? This is the PHP code https://stackoverflow.com/questions/44916337/php-count-all-elements-in-an-array – Mason Peace Jul 07 '17 at 00:42
  • @MasonPeace I think as it is not exactly related to this question you might want to open another topic and try to be specific the most specific in your question. – Anwar Jul 07 '17 at 09:09
  • I created another topic. Can you please help me? https://stackoverflow.com/questions/44988355/php-url-rewriting-not-working-correctly – Mason Peace Jul 08 '17 at 16:30
0

You can do this by using an intermediate page, say index.php and apply .htaccess rewrite only for index and load page based on GET[] on index.php.

You can refer this: URL rewriting in PHP without htaccess

J Shubham
  • 609
  • 7
  • 21
  • This is the PHP code that is generating the pagination links. Can you please take a look at this? https://stackoverflow.com/questions/44916337/php-count-all-elements-in-an-array – Mason Peace Jul 05 '17 at 17:43