Mobile app version of vmapp.org
Login or Join
Odierno851

: How to implement user-friendly paths in PHP without creating a file or directory for each page? NOTE: My terminology may not be entirely correct. I believe StackExchange does something like what

@Odierno851

Posted in: #Apache #Handler #Php

NOTE: My terminology may not be entirely correct. I believe StackExchange does something like what I am asking, in case this can make things clearer.

Using PHP on Apache, I would like to handle URLs such as:
www.site.com/info/<brand>/<model>/<page>

For example with <brand> = toyota, <model> = camry, <page> = 'safety'

And have all the URLs starting with 'info' (let's say) be handled by a
single PHP file like 'info.php' without the URL in the location bar changing.

In other words, someone navigates to:
www.site.com/info/toyota/camry/safety

But the code to handle this is in info.php but the URL in the browser stays
as is. I do not want to have to create all the /toyota/camera/safety directory
structure (there would be 1000s).

My guess is that info.php would get the path components as parameters in
order to know what content to serve. So the internal invocation invisible
to the user would be:

info.php?brand=toyota&model=camera&page=safety


Seems like the question here are doing something similar since they get URLs like:
webmasters.stackexchange.com/questions/13978/how-to-implement-user-friendly-paths-in-php-without-creating-a-file-or-directory

and I presume the path there is not actually created on the server.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Odierno851

1 Comments

Sorted by latest first Latest Oldest Best

 

@Becky754

Because you're using Apache, you want mod_rewrite, which is usually available by default. mod_rewrite lets you rewrite requests as they come in. It can make sub-requests, change the current request, or even redirect the user somewhere else. It's an exceptionally powerful tool with a syntax that can be kind of obtuse at times.

You'll want to set up RewriteRules. RewriteRules use the PCRE regular expression library, so if you've used regexes in Perl or the preg family of functions in PHP, you should be pretty familiar with the syntax.

There are two common patterns when making pretty URLs with mod_rewrite and PHP. In the first, you'll pick a "mount point" under which all the pretty URLs will live, and instead invoke a PHP script that is given the rest of the path after the mount point. For example, a call to /info/toyota/camry/saftey could be rewritten to /info.php/toyota/camry/saftey.

The PHP script would then access $_SERVER['PATH_INFO'], which would contain /toyota/camry/saftey, and process that value as it needs to. Yes, this will totally work. Try it out yourself with a phpinfo script. You'll find the bits after the file extension inside PATH_INFO.

Here's an untested but probably working example:

# ask mod_rewrite to turn itself on
RewriteEngine On
# If the request is not for an actual file, then...
RewriteCond %{REQUEST_FILENAME} !-f
# ... capture the part of the path after /info/ and replace it
# with a call to the PHP script. In the regex, .+ means "Any
# character, one or more times"
RewriteRule ^/info/(.+)$ /info.php/


The second pattern is to set up a rewrite rule to actually extract parts of the path and turn it into a plain old regular query string. This would be best if you have an existing script and don't want to make changes to it. It can also be more complex, as you'd need to write one rule per set of candidate URLs.

Here's another untested but probably working example.

# /info/foo => info.php?brand=foo
# [^/]+ means "Any character that is not a /, one or more times
RewriteRule ^/info/([^/]+)$ /info.php?brand=
# /info/foo/bar => info.php?brand=foo&model=bar
RewriteRule ^/info/([^/]+)/([^/]+)$ /info.php?brand=&model=
# /info/foo/bar/baz => info.php?brand=foo&model=bar&page=baz
RewriteRule ^/info/([^/]+)/([^/]+)/([^/]+)$ /info.php?brand=&model=&page=


This is just a tip of the mod_rewrite iceburg. There are lots and lots and lots of tutorials and guides out there that can help you along.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme