I have a question regarding mod_rewrite. I know in htaccess I can do the <IfModule>
, but what about开发者_如何学运维 in PHP? In html I am writing out my links like projects/24
which really is action=projects&id=24
. If the server someone installs this app on doesn't have mod_rewrite, how in PHP do I change the links to just GET vars?
use apache_get_modules()
Edit: I'm not sure which handicap voted down the correct answer, but he apparently can't rtm.
Here is some code to illustrate:
<?php
if (in_array("mod_rewrite", apache_get_modules())) {
echo 'has rewrite';
} else {
echo 'no rewrite';
}
?>
Use this to decide which kind of link you want to write.
how in PHP do I change the links to just GET vars?
Just make your all your links using variables.
And choose their appearance based on the some configuration setting, like Wordpress does.
There are workarounds using ErrorDocument and handling the failed requests in userland (= php script). But this will (1) pollute the log, and (2) does not work with POST requests (depending on server). Wouldn't bother with that.
A practical alternative is utilizing PATH_INFO
for clean urls in combination with one or multiple root scripts. You will need to use it with e.g. +MultiViews
or SetHandler
or DefaultType
and ForceType
(which can be used to plant a php script sans extension). [1]
But oftentimes those are disallowed directives too, if not even mod_rewrite is enabled.
[1] Don't forget a FilesMatch section to limit the effect, or disable it in subdirectories again.
I'm not sure exactly what you're going to end up with, but this might push you in the right direction:
http://www.tutorio.com/tutorial/php-alternative-to-mod-rewrite-for-se-friendly-urls
There are of course other problems with this, the biggest being problems caching and indexing these sorts of URLs in search engines.
精彩评论