What is the best way to organise a project for URLs?
I am working on a web portal (PHP/MYSQL) which have 3 sections photos, videos, articles. Each section has its own submission form using which user will submit photos, vid开发者_开发技巧eos or articles corresponding to their accounts.
So, that URLS which I thought should be like
- http://example.com/
- http://example.com/about/
- http://example.com/contact/
- http://example.com/help/
- http://example.com/photos/
- http://example.com/photos/browse/
- http://example.com/photos/submit/
- http://example.com/photos/edit/
etc.
Same as for videos and articles sections. Now I am confused here to what to use.
- Should I use .htaccess to create such type of URLs?
- Or I should use folders with index.php in it for /photos/ or /photos/browse/ like this. All folders will have index.php so they will be accessed by folder names and URL will look like this http://example.com/photos/browse/
- Or I should go traditionally and use photos.php and browse-photos.php . But this method not looks good and organized.
I can achieve this by htaccess easily but some peoples says that it will slow down your site, because it will process htaccess every time a URL will be requested.
I have one more question, how the sites like digg, flickr does this URL formation. These are pretty big site with millions of page-views a day, so are they not using htaccess?
Please guide me!
Thanks.
You can use .htaccess and apache's mod_rewrite (or similar for other web servers) for this. With mod_rewrite you'll be rewriting incoming urls to fit your script schema. The slowdown is negligible. Most big sites use this kind of trick.
So for a rough example, lets say you have the following working application:
./index.php
./index.php?section=about
./index.php?section=contact
./index.php?section=help
./photos.php
./photos.php?section=browse
./photos.php?section=submit
./photos.php?section=edit
Then you can put this in .htaccess
# Turn on URL rewriting
RewriteEngine On
# These are very verbose to show the point. Later you can make them smarter
RewriteRule about index.php?section=about
RewriteRule contact index.php?section=contact
#all under photos/* goes to photos.php
RewriteRule photos/(.*) photos.php?section=$1
And then place your code in the right places in index.php and photos.php
I can achieve this by htaccess easily but some peoples says that it will slow down your site, because it will process htaccess every time a URL will be requested.
For very, very, very high-traffic sites it is indeed important to see that there are not too many regex-based rewrite rules because they are somewhat performance intensive.
Most of us don't need to worry, though. Go with Apache.
.htaccess most likely won't be your bottleneck, if you need really high performance you won't get around measuring yourself where the problems are. Really big sites are not a good reference, at that scale the problems are very different.
The most flexible and common way is to use mod_rewrite to redirect to one page that then calls the appropriate part of your application.
I would not worry about performance at this point, but more about flexibility and maintainability.
You can also put your rewrite rule in the apache main config and save the overhead of .htaccess, that should be a bit faster
Using htaccess will make it much more simple to organize your files than with lots of folders. Yes, it has an impact on performance, but, unless it's going to be a really high-traffic site, the impact won't be noticeable. It you're worried about performance, keep in mind that bad code can slow down your site a lot more than any rewrite rules.
精彩评论