?
Solved

Apache mod_rewrite not following [L] option

Posted on 2007-08-09
8
Medium Priority
?
235 Views
Last Modified: 2010-04-20
Windows Apache 2.2.4
mod_rewrite enabled

I'm trying to do a fairly simple rewrite scheme, but I'm having trouble figuring out why this isn't working:

RewriteEngine On
RewriteRule ^ajax/local_components/.* /Automation2007/main/local_components/ [L]
RewriteRule ^/$ /Automation2007/main/ [R=301]
RewriteRule ^main$ /Automation2007/main/eng/index/ [R=301]
RewriteRule ^main/$ /Automation2007/main/eng/index/ [R=301]
RewriteRule ^main/.* local_handler.php

The goal is to have all requests to Automation2007/main/* go to a php handler EXCEPT for requests that initially start as Automation2007/ajax/local_components/.

Basically, I want to let AJAX requests go through, but want all other requests to go to the handler. Therefore, I rewrite requests to give access to components that otherwise would not be accessible.

The problem is that it does not seem to be following the [L] directive. If I try the following url:

http://localhost/Automation2007/ajax/local_components/

It passes the request to the handler, which is not what I intend. Instead, I want to see the contents of the folder at Automation2007/main/local_components. Shouldn't the ajax rewrite rule, if it matches, be the last rewrite performed due to the [L] directive?

What am I doing wrong?
0
Comment
Question by:M3Mania
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:HackneyCab
ID: 19666782
No, the L flag tells Apache to process no more rules, and resubmit the newly rewritten URL. Then the newly rewritten URL goes through the rules from the start.

The only way to stop infinite loops and unwanted processing of URLs is to create a RewriteCond directive that only permits the RewriteRule to execute if a certain condition is met.
0
 

Author Comment

by:M3Mania
ID: 19666888
What condition would I put in to achieve what I'm looking for then? I tried some conditions, but they didn't seem to work...
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 19672815
Actually, reading your ruleset again, I can't see why /Automation2007/ajax/local_components would ever get matched by the rules than redirect to the PHP file. Are those the only rules in your .htaccess file? Are there any others interfering.

I can't tally your description with your rules, so can you describe again how you want URLs to be rewritten. Describe them as "URL should appear to visitor as domain/path/file" and "location on server should actually be /path/file".
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:M3Mania
ID: 19674053
To visitor:
http://localhost/Automation2007/main/...

Location on server should actually be:
local_handler.php (in root)

To visitor:
http://localhost/Automation2007/ajax/...

Location on server should actually be:
http://localhost/Automation2007/main/...

This seems strange, but the reason is that the handler needs to take all inbound requests that are in the 'main' directory, while ajax requests need to avoid the handler and go straight to the requested file.
0
 

Author Comment

by:M3Mania
ID: 19674062
Oops, forgot to answer the entire question:

These are the only rewrites in the file. If I understand your explanation properly, then it makes sense for requests to ajax to end up at the handler. Ajax requests are resolved to main, which in turn resolves to hander (the final rule resolves anything starting with main/ to the handler).

Also, I haven't added any other rules in the main http.conf file, so that is probably not causing a problem.
0
 
LVL 16

Accepted Solution

by:
HackneyCab earned 2000 total points
ID: 19675483
Ah, I see now. I don't think this can ever work, though, because each time Apache rewrites a URL, that new URL eventually gets run through the rules again.

So everything will end up being sent to the local_handler.php file, because there's no way for Apache to know whether the request originally asked for main or ajax.

The only way I can think of to tell Apache that there is a difference is to set a query-string when one of the two URLs is originally requested, and then use RewriteCond to check for that query string to see what to do. Like this:

RewriteRule ^Automation2007/ajax/(.*)$ /Automation2007/main/$1?run_handler=false&%{QUERY_STRING}

RewriteCond %{QUERY_STRING} !run_handler=false
RewriteRule ^Automation2007/main/.* local_handler.php

This sets a new query-string variable called run_handler to false if the user originally asks for ajax, so that the rewrite condition does not allow a further rewrite to the local_handler.php the next time the rules are processed.

Note that I've assumed that you want to capture what comes after /ajax/ and add it to the new URL after /main/, but if this is not the case, just remove the $1 from the new URL.
0
 

Author Comment

by:M3Mania
ID: 19675947
I don't think that will work for what I'm trying to accomplish. I think I'll have to use the local_handler.php to handle ajax requests, too.

Still, you did answer my question... even if the answer wasn't what I was hoping it would be. I'll accept your answer as the solution. Thanks for keeping up with it.
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 19676337
Good luck in finding something that works for you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question