1

Imaging we are at home page: www.example.com

we have a hyperlink on this page.

<a href="category/Pizzas">Pizzas</a>

when click this hyperlink, the url at address bar will be:

www.example.com/category/Pizzas

and it will show us the different kinds of pizzas. The problem is I don't have a path like category/Pizzas in my server. The way it works is to pass category and Pizzas to two query strings (page and cat), which will be stored in $_GET. So the real url is :

www.example.com/?page=category&cat=Pizzas

Then there is a function to render the Pizzas page.

You can image we have some other hyperlinks on the main page and they have different urls. For example:

<a href="category/Salads">Salads</a>
<a href="category/Wraps">Wraps</a>
<a href="category/Grinders">Grinders</a>

when you click it, query strings(page and cat) will capture the value from url and render accordingly.

The part I don't understand is how and where to define the query string (in this case: page and cat) and capture value from URL like category/Pizzas so we can have page=category&cat=Pizzas. There is no HTML form for these two query strings in source code. It just works perfectly.

ps. if you are interested, here is the project I'm talking about. This is not my project. https://github.com/nelsonreitz/project0

1
  • 3
    With an apache server, this would be done with mod_rewrite. Commented Sep 19, 2015 at 9:02

3 Answers 3

2

In that repo, take a look in public/.htaccess This is where the magic happens. It's the web server which actually does the job of mapping the url to parameters. The code looks like this:

RewriteEngine on
RewriteBase /
RewriteRule ^category/(.*)$ index.php?page=category&cat=$1 [L]

What that's doing is saying any url that looks like category/ will be rewritten to index.php?page=category&cat=

So index.php actually handles all those requests, and has the correct GET value - even though the url used is different.

Sign up to request clarification or add additional context in comments.

Comments

1

If you're using Apache you can do this with mod_rewrite. You can learn more about here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

The example rule would be:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?page=$1&cat=$2 [L]

Comments

0

well he means he don't know as to capture the $_GET values of www.example.com/category/Pizzas in php source code.

this is all done with apache mod rewrite or nginx:

on your index.php page you can define it:

<?php
if (isset($_GET['page']) && isset($_GET['cat'])) {
$page = $_GET['page']; // category
$cat = $_GET['cat']; // Pizzas
// From here you can start using both strings
}
?>

php will get this:

www.example.com/index.php?page=category&cat=Pizzas

and user will see www.example.com/category/Pizzas on URL (thanks to mod rewrite):

RewriteEngine on
RewriteBase /
RewriteRule ^category/(.*)$ index.php?page=category&cat=$1 [L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.