1

When browsing sites in the past I always assumed that example this url: http://example.com/folder/page/something-random that the something-random was an actual folder, and was created in the page.php area.

But recently I've gotten into frameworks and gotten into .htaccess and notices that those things don't necessarily have to be done using a framework, and was wondering how it works?

Is it some type of edit done in the .htaccess to make the /something-random come up as a GET php variable?

If the question is a bit confusing I apologise.

Jack Hales
  • 1,574
  • 23
  • 51
  • I think you're talking about 'slugs', you can read a good explanation here: http://stackoverflow.com/a/19335586/3779933 – Michael Millar Apr 03 '16 at 16:49
  • Yes, you have to put `RewriteEngine On` and define a `RewriteUrl "your rule"` in the `.htaccess` file and implement some logic in PHP. – cezar Apr 03 '16 at 17:02

1 Answers1

2

You dont necessary need to create folders for url to look pretty. if you have dynamic urls, i would advise use of .htaccess rewriting rules for url it would make the url look as if its a path yet its a variable

.htaccess are useful for enforcing the Pretty URLs. They are useful in that they always helps you to boost up search engine page ranking and they are also user Friendly. It helps to make URLs looks neat on the browser address bar And not necessarily does it have to be an actual folder.

An example

Original URL.

 http://flickr.com/users.php?id=username&page=2

Rewriting Friendly URL.

 http://flickr.com/username/2

In the .htaccess file you would have something like this for it to happen

.htaccess file

//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?user=$1

//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2

To capture the variables in php you could have something like

php file

<?php

$key=$_GET['key'];

if($key=='home')
{
include('home.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>

You could learn more about usefulness of .htaccess as you google is usage for example my reference pretty url on 9lessons

Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46