Forum Moderators: phranque
RewriteRule ^(.*)\.php$ index\.php?term=$1 [NC,L]
This works pretty well. However it's rewriting every page on the server.
now this isn't necessarily a bad thing. but I'd like to be able to access files files other than the index page.
Is there a way manually include pages that should not use the rewrite rule? or maybe exclude certain directories?
or do i need to maybe add a directory in front of the rewrite rule so it's not rewriting everything.
what are my options.
Yes, and maybe, respectively. The answers depend on exactly what you want to do, and why you want to do it.
For general information about including and excluding URL-paths from being rewritten based on various criteria, see the RewriteCond [httpd.apache.org] directive in mod_rewrite. And of course, you can make the URL-pattern in your RewriteRule more selective, or move the code to an .htaccess file in only the directory you want it to affect.
This is server configuration, and also programming. So defining the problem in precise terms which can be implemented in code is the first, most difficult, and unavoidable step.
Jim
The solution presented above is Rewrite all requested URLs which do not exist as 'real' files on this server. That works, but can have unexpected results if a file is unexpectedly missing or deleted. It requires that your script correctly handle requests for *any* URLs that might be requested -- For example, you need to be sure that robots.txt, favicon.ico, /w3c/p3p.xml, and many other 'customary and expected' files exist, OR that your script can properly handle requests for them.
A way out is to put something in the URL itself that can be used to decide, "Do I or do I not want to rewrite this URL?" Examples would be to prepend a subdirectory path to the URL, or to rewrite only certain kinds of resources, say, ".htm" pages, or ".gif" images, or to rewrite only URLs containing the string "foo" -- It all depends on what you're trying to do.
But it's a good idea to try to think in mod_rewrite's own terms, and learn to think of URLs as both strings of characters and as members of groups and classes including other URLs. Sorry I can't express that more clearly if it's not...
Jim
To make the rewrite(s) efficient, and prevent looping you may need to use THE_REQUEST and redirect requests for ?term= to keyword+keyword.php, then rewrite keyword+keyword.php to ?term=.
What you are looking for is a little more complicated than a single rule can provide. Please, keep in mind, the redirect/rewrite will not increase Search Engine rankings (per se), because a Search Engine will not ever complete your form to find the 'new' URLs.
I also recommend, when redirecting, you redirect to /results/keyword+keyword.php OR /search/ OR /something-else/ so you will know which directory to redirect/rewrite, and eliminate the need for 'guessing' at which files OR directory should have the rule(s) applied. (The + symbol might not be the best delimiter either.)
Justin