0

I want to remove the = and ? from the 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

This is the PHP code that generates the URL.

 <?php
   class Pagination {
   public $current_page;
   public $per_page;
   public $total_count;
   public $pages_articles;

  public function __construct($page=1, $per_page=20, $total_count=0) {
  $this->current_page = (int)$page;
  $this->per_page = (int)$per_page;
  $this->total_count = (int)$total_count;
  $this->pages_articles=array(
   '<div class="article-loop"><img src="http://i.imgur.com/CmU3tnl.jpg"> </div>',
'<div class="article-loop"><img src="http://i.imgur.com/TDdxS9H.png"></div>',
 '<div class="article-loop"><img src="http://i.imgur.com/39rpmwB.jpg"></div>',
 '<div class="article-loop"><img src="http://i.imgur.com/1lBZQ1B.png"></div>',
 '<div class="article-loop"><img src="https://i.imgur.com/Y5Ld4Qfh.jpg"></div>',
 '<div class="article-loop"><img src="http://i.imgur.com/8HumESY.jpg"></div>',
 '<div class="article-loop"><img src="http://i.imgur.com/CqCZBvk.png"></div>',
 '<div class="article-loop"><img src="http://i.imgur.com/wQVPRVp.png"></div>');
 }

   public function offset() {
 return ($this->current_page - 1) * $this->per_page;
  }
   public function total_pages() {

 return ceil($this->total_count/$this->per_page);
  }
   public function previous_page() {
 return $this->current_page - 1;
  } 
   public function next_page() {
 return $this->current_page + 1;
  }
   public function has_previous_page() {
 return $this->previous_page() >= 1 ? true : false;
  }
   public function has_next_page() {
 return $this->next_page() <= $this->total_pages() ? true : false;
  }
 }
   $page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
   $per_page = 3;
   $total_count=8;
   $pagination = new Pagination($page, $per_page, $total_count);
   ?>

 <html>
 <body>
 <div>
 <?php
   $i = $pagination->offset()  ;
  $limit = $pagination->per_page;
   while($i<$pagination->total_count && $limit>0) {
    echo $pagination->pages_articles[$i]."<br>";
    $i++;
    $limit--;
   }
  ?>
  </div>
  <ul>
    <?php
   if($pagination->has_previous_page()) {
    echo '<li style="display:inline"><a  href="index.php?page='.$pagination->previous_page().'">&laquo;</a></li>';
    } else {
    echo '<li style="display:inline" class="disabled"><a href="#">&laquo;   </a></li>';
   }
   for($i=1; $i<=$pagination->total_pages(); $i++) {
    echo '<a href="index.php?page='.$i.'"><li style="display:inline; margin-left:5px; margin-right:5px">'.$i.'</li></a>';
   }
  if($pagination->has_next_page()) {
    echo '<li style="display:inline"><a href="index.php?page='.$pagination->next_page().'">&raquo;</a></li>';
   } else {
    echo '<li style="display:inline" class="disabled"><a href="#">&raquo;</a></li>';
   }
  ?>
 </ul>
 </body>
 </html>

https://3v4l.org/i4GZQ

Mason Peace
  • 93
  • 1
  • 10
  • 3
    You are expected to try to **write the code yourself**. After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Jul 05 '17 at 21:33
  • Please add the code to the body of the question, not as external links that can expire, might be located on sites that are down or end up deleted, or are controlled by unknown third parties. You can edit your question to add any code that's relevant to the question at hand that helps people investigate, reproduce and resolve your problem. – tadman Jul 05 '17 at 21:33
  • Frameworks do this through a routing layer, and that routing layer depends on having `.htacess` type rules to rewrite everything to `index.php`. There's no way around this. That's just how your server works. If it's not configured to understand that `/test/3` is actually a reference to something other than that literal path, it will just 404. – tadman Jul 05 '17 at 21:34
  • Know that this is somewhat of a repeat question asked earlier today: https://stackoverflow.com/questions/44932283/best-way-to-remove-and-from-get-method-in-php – Paul T. Jul 05 '17 at 21:40

1 Answers1

1

Your webserver has to map the URL onto your PHP program somehow.

The traditional way is to use a .php file extension and map the URLs directly onto it, but that won't give the effect you want.

To get that effect; the use of mod_rewrite is common, and putting mod_rewrite directives in a .htaccess file is also common. This is what the Apache manual has to say about it:

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

So you can follow that advice and put the mod_rewrite directives you don't want to put in .htaccess in your Apache configuration files.

Assuming you are using Apache and have access to those files.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • how would I rewrite the url with mod_rewrite? I want to hide ?page= – Mason Peace Jul 05 '17 at 21:40
  • https://stackoverflow.com/tags/mod-rewrite/info links to everything you need to know. Since you'd ruled out .htaccess I assumed you had identified mod_rewrite as a solution and thus found the many, many guides on Stackoverflow about it. – Quentin Jul 05 '17 at 21:41
  • I was told it would be cleaner to do it through php and not htaccess. – Mason Peace Jul 05 '17 at 21:43
  • You can do *bits* of it in PHP (See the "Common mod_rewrite usage scenarios and questions" section of the link above for hints about what the different approaches are), but you can't do all of it (see the first line of this answer) and "cleaner" is pretty subjective. – Quentin Jul 05 '17 at 21:45
  • You can use mod_rewrite without having to suffer the cost of directory scans incurred with .htaccess. Place the rewrite rule in the block of a vhost for example. It is the `AllowOverride` that people are really talking about - directory level overrides you want to avoid... What Quentin said... – ficuscr Jul 05 '17 at 21:47
  • I guess I'll go with htaccess method since it's much easier and sounds like it will be "cleaner" than the php approach. Thanks. – Mason Peace Jul 05 '17 at 21:48