InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Exposing Clean Resource Paths in PHP
PHP

Exposing Clean Resource Paths in PHP

InfinityCoder December 20, 2016

You want your URLs to look clean and not include file extensions.

Use Apache’s mod_rewrite to map the path to your PHP script:

1
2
3
4
5
RewriteEngine on
RewriteBase /v1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?PATH_INFO=$1 [L,QSA]

Then use $_GET[‘PATH_INFO’] in place of $_SERVER[‘PATH_INFO’]:

1
$request = explode('/', $_GET['PATH_INFO']);

Use mod_rewrite to expose elegant URLs, such as /v1/books/9781449363758, even when there isn’t a file at that specific path.

Without this, you end up with the more clumsy URL of /v1/books.php/9781449363758. If you’re running another web server, such as nginx, use its own syntax for handling this type of URL mapping.
The code in the Solution tells Apache that when it doesn’t find a file or directory at the requested path, it should route it to index.php instead.

Additionally, so you can still read in the original URL to properly process the request, extract the path and pass it in as the PATH_INFO query parameter.
Inside your script, parse the path into its components by breaking it apart on “/”:

1
$request = explode('/', $_GET['PATH_INFO']);

This breaks a request for /v1/books/9781449363758 into:

1
2
3
4
5
Array
(
    [0] => books
    [1] => 9781449363758
)

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Validating Form Input: Checkboxes in PHP
You want to make sure only valid checkboxes are checked. …

Validating Form Input: Checkboxes in PHP

Parsing Complex XML Documents in PHP
You have a complex XML document, such as one where …

Parsing Complex XML Documents in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap