Forum Moderators: phranque

Message Too Old, No Replies

Help with rewriterule

         

grey580

10:09 pm on Apr 11, 2007 (gmt 0)

10+ Year Member



Here is my rewrite rule.

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.

jdMorgan

10:55 pm on Apr 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> 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?

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

phranque

11:03 pm on Apr 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you can test for the existence of the file before running the rule:
RewriteCond %{REQUEST_FILENAME}!-f

(there should be a space between the '}' and the '!')

jdMorgan

11:49 pm on Apr 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but that is absolutely the the second-slowest thing that can be done with mod_rewrite... The slowest being rDNS checks invoked by remote hostname lookups.

It is like using ".*" patterns -- easy but inefficient.

Jim

grey580

12:08 pm on Apr 12, 2007 (gmt 0)

10+ Year Member



phranque thanks alot.
that did the trick quite nicely.

and besides .* what should i be using? any suggestions?

phranque

12:34 pm on Apr 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



and besides .* what should i be using? any suggestions?

depends on the specifics.
for example you might be able to use something like this instead:

RewriteRule ^([^\.]*)\.php$ index\.php?term=$1 [NC,L]

grey580

1:51 pm on Apr 12, 2007 (gmt 0)

10+ Year Member



well basically i want to pass on search terms to a script.

so if i'm searching for paris hilton for example.
I pass along Paris+Hilton to the script then rewrite the url to www.website.com/Paris+Hilton.php instead of www.website.com/index.php?term=Paris+Hilton.

g1smd

12:04 am on Apr 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have a go at writing it, test it, and then post it here.

jdMorgan

3:25 pm on Apr 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The main thing we appear to be struggling with here is that mod_rewrite sees only requested URLs and other information sent by the client in the HTTP headers. Therefore, any problem and its solution must be defined in terms of what URLs, groups of URLs, kinds of URLs, etc. are to be rewritten.

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

jd01

4:31 pm on Apr 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have done something similar to what you are requesting.

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