Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone prompt me in the right direction to use mod_rewrite?

I am a bit new to this but I am trying to learn. I know that in PHP we can have a URL that ends in ?id=something but it seems to me that some websites are doing it just forward slashes. Is this possible using PHP? I saw these questions that seem to say it is possible but I haven't cracked it yet.

Can someone prompt me in the right direction for mod_rewrite? I would like to change the following URL

http://www.example.com/all-products/?onsale=true&topic=health&item=adaaos45

into

http://www.example.com/all-products/items-on-sale/health/adaaos45

Can you point me into the right way?

like image 653
user3143680 Avatar asked Dec 30 '13 15:12

user3143680


1 Answers

What you are looking for is Apache Url Rewriting.

I will break it down a bit for you to help you understand, it helps to know a bit of regex.

there are a lot of answers here that discuss the method, but to sum it all up, you need to do three things.

# Switch on URL Rewriting
# Choose the URL that you want to display  
# Point it to the true URL that gives the information.

Options FollowSymLinks
RewriteEngine On

RewriteRule ^all-products/items-on-sale/health/adaaos45/?$ all-products/?onsale=true&topic=health&item=adaaos45 [NC,L]

Now of course, if you would want to match any results for the variables, you need to match the word in regex, and remember it, and use it in the last part of the line. But this should get you started on understanding what is going on.

With this code in your .htaccess, browsing to

http://www.example.com/all-products/items-on-sale/health/adaaos45

will show you the content that displays on this page.

http://www.example.com/all-products/?onsale=true&topic=health&item=adaaos45

like image 194
Draco Avatar answered Sep 27 '22 21:09

Draco