I'm building a small PHP framework for the websites I create and wanted to know if I can rely on mod_rewrite being availabe on most hosters.
Is ist even a good idea using it? There are many good points already (one entry point, "beautiful", readable URLS)
Where is it not availabe?
Should I include an alternative in the frame开发者_C百科work if mod_rewrite is not available on that hoster?
mod_rewrite is generally available. If it is not available on your hosts' servers, drop them.
The MOD_REWRITE module is generally avaiable on most Apache hosts these days. However, it's not THAT hard to work around its absence, and by doing so you ensure that people stuck on a/with a host/IT staff who've turned it off, or a host/IT staff running IIS, etc.
Most new frameworks or applications built today use mod_rewrite to intercept all URL requests through a single front loading PHP file (called Bootstrapping, Frontloading pattern, etc.). The URL portion that's not the domain name is parsed into some object, and that object is used whenever you need access to the URL.
When MOD_REWRITE is NOT available, URLs in the form of
http://example.com/index.php/foo/baz/bar
are used instead. A request for the above URL is handled by the index.php file. Then, you can parse one of the the server variables like
$_SERVER['REQUEST_URI'], $REQUEST['PATH_INFO'], etc.
into some object (the same object you'd use if you had mod_rewrite) and use that object whenever you needed access to the URL information.
Either way, the rest of your framework/application just accesses the object, and doesn't have to worry about how the information got in there.
Additional Reading
If you’re developing an application that is publicly available, you should consider that mod_rewrite might not be available and support an alternative. AcceptPathInfo
is one.
mod_rewrite is just for apache so if you need to work on IIS or something else you are going to have problems, but in PHP you also can use this:
private function getURI()
{
$uri = "";
if (isset($_SERVER['PATH_INFO']))
$uri = getValueFrom($_SERVER, 'PATH_INFO', @getenv('PATH_INFO'));
elseif (isset($_SERVER['ORIG_PATH_INFO']))
$uri = getValueFrom($_SERVER, 'ORIG_PATH_INFO', @getenv('ORIG_PATH_INFO'));
else
$uri = "";
$scriptname = basename($_SERVER['SCRIPT_FILENAME']);
if (strpos($uri, $scriptname) > -1)
$uri = substr($uri, strpos($uri, $scriptname) + strlen($scriptname), strlen($uri));
return $uri;
}
Change the URLs is a good practice, actualy is wide used on the web and is a SEO recomendation because is better have this:
domain.com/aboutus
to this
domain.com/index.php?app=main&action=show&id=1
It's available in most servers -- but if you suspect it's not, you should make a dynamic way to fetch links -- you could then have a predefined constant that you modify before everything that says whether or not mod_rewrite is available. You can then fetch the URLs using that constant as the way to determine what happens.
I think any decent application should make use of mod_rewrite
, and most of web hosts are offering this. If they don't offer, get off them.
Also, not just mod_rewrite
is a good option to use but also other php settings like this php_value
max_execution_time
and so on to be sure you are customizing your environment to fit your application purpose.
精彩评论