I want to have "pretty" and SEO oriented URLs in my site.
I've build up my own tiny framework for this site and almost everything is complete now.
One thing I'm still puzzled up is the pretty/SEO URLs system that I will use. I know there's many way to achieve this and I'm looking to balance best practices/ease of implementation on this one.
So far I'm thinking to have all URLs of the site to point to a specific PHP file (let's say index.php) that will contain a file/URL dictionary that will direct traffic to the correct file.
I'm really not sure if it's a good approach... Anyone have a better way to do this? The only thing I really want to avoid is to only do this in an .htaccess...
You'll need an .htaccess file (but you won't need to change it each time you add a page):
Now in your
index.php
you can access the requested url in$_GET['url']
, map it to the correct file and then include it.Note: Put the
RewriteBase
comment in there in case you need to uncomment it as some configurations require this.An alternative to routing through your index.php page and the rewrite rules etc people have been posting is multiviews. It is by far the easiest way to do what you want and will let you convert your site to use pretty/seo urls really easily and intuitively without complex htaccess rules and having to maintain routings (in .htaccess or a db)/a complex site structure.
To enable multiviews, edit your httpd.conf or vhost file for the directory you want to enable it for: (if you're on shared hosting it should still be possible providing your host allows you to do a local override in your .htaccess file -
Options +Multiviews
)... And then you're done! Now take a look at an example of it in action:
http://{yoursite}/page.php can now be accessed as http://{yoursite}/page or even http://{yoursite}/page/variable or even http://{yoursite}/page/variable/variable/variable/variable !
What apache is doing is looking for the closest file match for the path sent to it and calling that file. All that is required to make these URLs usable is a function within your framework to extract data from the URL. Here's a basic function I have used before which could be adapted to your needs, it extracts the variable in the URL at $position:
You could adapt it to parse URLs however you like (you don't have to separate variables based on a /, page-variable.html can also route to page.php), as long as the target file is the closest matching file for the URL entered.
Of course, there are downsides to using multiviews and it goes against the philosophy of directing all requests through your index.php file - but it does answer your request for simplicity and not having to maintain a htaccess file and is good to know about/consider.
Directing everything to index.php and then routing from there is a good, clean way to do it. I have something very similar, I:
For example, browsing to:
www.blah.com/shop/browse/cakes
Would call index.php, which would include shop.php and instantiate a class called Shop. Would try to call a function on Shop called browse and would pass it a parameter of "cakes".
This is a simplified example but you get the idea. Convention over configuration makes the URLs and the code clean.