-6

I have never used .htaccess is my life now I am learning it but I find it very difficult to percieve.

Can anyone tell me steps to rewrite url

For instance

From this

Www.example.com?s=abc

To this

Www.example.com/abc

Matt
  • 37
  • 1
  • 5

1 Answers1

3

The .htaccess is actually only 1 half of the job.

This .htaccess will achieve what you want (I think)

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.+)$ index.php?request=$1 [QSA,L]

This .htaccess turns all your requests into index.php?request=myurl on the background, while on the front it says mydomain.com/myurl, however, it will not do this for the extensions as stated on the 1-to last line. (otherwise you'll get into trouble when trying to load css, js, images, etc). Add any extension you wish to exclude from the routing there.

However, there is still the part of catching this in PHP. Everything will route through your index.php now. This means you'll need a script to "catch" the request and manually include the files needed.

I'll paste a very simple example for you here:

<?php 
  $getRequest  = explode('/', $_GET['request']);
  $page = $getRequest[0];

  if( file_exists("/mypages/$page.php") ) {
    require_once('myheader.php');
    require_once("/mypages/$page.php");
    require_once('myfooter.php');
  }
  else{
   echo "page not found";
  }
?>
NoobishPro
  • 2,539
  • 1
  • 12
  • 23
  • rather adding new answer its better to mark it Flag as duplicate. – Abdulla Nilam Dec 26 '16 at 00:39
  • @AbdullaNilam I would have if I knew it was one... – NoobishPro Dec 26 '16 at 00:39
  • 1
    @Babydead You mean you thought you are the first person to answer such question on SO? – EhsanT Dec 26 '16 at 00:41
  • 1
    @EhsanT no, but the first possible duplicate that has been linked is not related AT ALL, and the second one is way to complicated for a newbie. Heck, I'm a senior programmer and I still have trouble understanding that load of information. I'm pretty sure this stuff doesn't help him out, and the goal of SO is helping people out. Nobody is going to get answers by asking more specific questions on such old and huge posts. I've been there and it's very demoralizing. – NoobishPro Dec 26 '16 at 00:45
  • 1
    OK, Fair enough. but with with a simple search the OP could find many questions like he asked. [Some](http://stackoverflow.com/a/37786107/1908331) and some with with simple answers like yours and some with more complicated answers like [this one](http://stackoverflow.com/a/16389034/1908331). – EhsanT Dec 26 '16 at 00:56
  • 1
    I have read those questions already and yess babydead I didnt understand those tons of content in those answers. This seems to be understandable. I really appreciate your help to newbies. Thank you very very much – Matt Dec 26 '16 at 10:32