3 liner htaccess

Hi guys, I’m having a big problem on the site where sometimes when I try to submit a web form, the request is suppose to send to itself, but then gets automatically redirected to a 404 page. It has been posted a while back on major forums but to no success. I’m still debugging the thing and hopefully you can help me figure it out.

Could anyone please explain what this htaccess rule is about? Or if you see anything wrong about it?

RewriteRule ^([^/]+)/$ /$1 [R]
RewriteCond %{QUERY_STRING} !mod= [NC]
RewriteRule ^([^/]+)$ /index.php?mod=$1 [L]

Our framework relies that the server would pass everything on index.php, from there we determine how to handle the request. But a peculiar thing happens. The log on firebug and LiveHTTPHeaders says that depending on the text being submitted, the web server might encounter a 404, therefore bypassing our mandatory index.php rule. If I’m correct, then its got to do with out htaccess, or how Apache is setup, which unfortunately for the later I have no clue.

Thoughts anyone?


Welcome back!

The problem is that you’re getting carried away with the /'s in the {REQUEST_URI}. Without a Last flag, that first RewriteRule is ANDed with the RewriteCond so, if a request is NOT made to a subdirectory, the second RewriteRule is NOT matched. Pure, simple logic.

Now, if your index.php is the handler file, all you need to do is check the query string as you’ve done and (if the index.php script hasn’t already been requested - i.e., escape the loop [and the mod key is not likely to be sufficient to do that] - redirect to index.php with the query string (and you’re not protecting any previous query string).

On the assumption that:

  1. You do NOT want to redirect away from your css, js, jpe?g and gif files (caps do matter, but are NOT allowed below),

  2. The mod key in the query string is sufficient to stop the loop, AND

  3. Prior query strings should be retained

RewriteEngine on
RewriteCond %{REQUEST_URI} !\\.(css|js|jpe?g|gif)$
RewriteCond  %{QUERY_STRING} !mod= [NC]
RewriteRule .? /index.php?mod=%{REQUEST_URI} [L]

Note, though, that the use of * in the RewriteRule will require index.php to handle the null value for mod.



Hi dk,

Thanks for your trouble, I appreciate it!

I used the code you provided and thought if it doesn’t work the way I need it, I’ll just edit from there. But firefox gave me a “page is not redirecting properly” page. So I wasn’t able to do much.

But from your explanation I think I know where to start. I’m just having trouble searching for right mod rewrite material to refer to.

Still, I’m having trouble relating the problem.

Say I’m on a url www.domain.com/write-artile. I fill up the form and the form sends to itself (same url/same php file). Now depending on the article content I provide, it might get save, or it might get redirected. Sometimes its a space character, or a punctuation, or we might have to rewrite a sentence.

My problem is that, I cant connect it to the RewriteRule bug. I just have a hunch that its the RewriteRule since our index.php doesnt get executed when the redirect fires.


My best thought is to use

RewriteCond %{REQUEST_FILENAME} !-f

which will test whether the file does (NOT) exist. Replace the file extension RewriteCond line with that one and it’ll avoid looping on any file which exists (the more common way to do that than what you’d started with).