-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

0

1 Answer 1

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"; } ?> 
Sign up to request clarification or add additional context in comments.

6 Comments

rather adding new answer its better to mark it Flag as duplicate.
@Babydead You mean you thought you are the first person to answer such question on SO?
@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.
OK, Fair enough. but with with a simple search the OP could find many questions like he asked. Some and some with with simple answers like yours and some with more complicated answers like this one.
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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.