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...
Well, there are some ways to do this.
Widely adopted is using .htaccess, which you said don't wanna use. OK.
You still have some options:
Well, everything above can be mixed on your implemetation if you want.
You can find a good article for this on a list apart.
http://www.alistapart.com/articles/succeed/
On this article the solution is a mix:
first check if a file exists (for example "about-us.php"); if yes, include the file contents, else
check db for this request (as a tip, you can have a field named "friendlyURL" on your main content tables). if exists, extract and display, else
show a 404 page. as a tip for this one, keeping the SEO feature in mind, I would recommend you to have a sitemap xml. If page is not found, you can check sitemap if is not a broken URL, like:
http://yourdomain.net/shoes/male/bro
you can check if some URL like: http://yourdomain.net/shoes/male/brown
and suggest it to your customers/visitors. Along with:
http://yourdomain.net/shoes/male/
http://yourdomain.net/shoes/
also a link for your HTML sitemap, and if you have a search feature on your site, also use it, display a link for user go to search page with that query.
http://yourdomain.net/search?q=shoes+male+bro
OR
[input type="text" name="q" value="shoe+male+bro"];
And another extra tech tip: make use of full-text search feature of your db if available.
A interesting reading comes from Rasmus Lerdorf, PHP creator: http://lerdorf.com/lca04.pdf (check page 34, about 404 redirects).
Very pretty routing is in Nette Framework see nette.org. Like your idea, every url goes to index.php. And there is routers, which direct what will happen, etc. http://api.nette.org/2.0/Nette.Application.SimpleRouter.html
Just to add another alternative, you might want to check out the RewriteMap directive.
I think you said that the pages could be static, right? Well, a solution that I use sometimes is:
This way, you can't run into issues like people accesing files like .htpasswd or sensitive data. The page /about would lead to /files/about.php
Create your files directory and new PHP (or HTML, XML, whatever - just change it or add it in the .htaccess as well) files. Nothing else than that.
Let me know if this is good wnough for you :)
P.S: If the pages have content from the database (like articles on a blog) it could be even easier to use an extra parameter and stick with .htaccess:
This way, the rules I said first time apply, but when you have an URL like /3/my_new_article it would point to /files/article.php?id=3
You would also have to parse the title of the article with PHP so it would only accept the characters in the regex, look nice and also contain only valid URL characters if you plan to change the regex.
Cheers!
you are probably looking for something like mod-rewrite that way you would have a link that looks like http://my.server.com/my_file_of_this_name
and with mod-rewrite it would turn it into http://my.server.com/index.php?path=my_file_of_this_name
where index.php knows that if path was a passed variable, look in the DB for the content or page matching 'my_file_of_this_name' and either display it or header("location: ???) it
If you want a seo urls there is a pretty good function to do with(out) htaccess ! it will be used as $_POST & $_GET . use it as $_SEO !! .. it uses /query-value/query2-value2/ $_SEO['query'] will return 'value'
check it at : http://mohammed-alashaal.blogspot.com/2013/07/php-seo-uri-without-htaccess.html