why does a matched rewriterule not work? - apache

I have the following .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^a/(.*)$ api.php?params=$1 [L,QSA]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I expect that when I go to /a/test that the server returns /api.php?params=test
Instead, the third rule is matched.
If I comment out the third rule, then the first rule works.
Why is that?

Eventhough you have the L flag, which stops rewriting for the current rewriting iteration, the result (the rewritten URI) will be put back into the rewrite engine, and will continue to do so, until the URI going into the rewrite engine comes out unchanged. So what's happening is the first rule gets applied, then api.php?params=test is put back into the rewrite engine, where the 3rd rule gets applied.
You can either turn off all looping, by passing through the URI if an internal redirect was made, by adding this right underneath RewriteEngine On:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Or add a condition to the 3rd rule so that it ignores requests to existing resources:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) app/webroot/$1 [L]
Or add an explicit condition to ignore api.php:
RewriteCond %{REQUEST_URI} !^/api\.php
RewriteRule (.*) app/webroot/$1 [L]

Related

Not able to track htaccess issue, How to implement IF ELSE in htaccess

I have a problem with htaccess to execute a rule and if rule matches then do not check for another rule. which i am trying to figure out from last few hours. Below is the sample code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/projectname/$
#RewriteCond %{REQUEST_URI} !projectname/(storage)$
RewriteRule ^(.*)$ /projectname/abc/dist/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} projectname/(storage)$
RewriteRule ^(.*)$ /projectname/storage/$1 [L]
Rewritecond %{REQUEST_URI} projectname/(.*)$
#RewriteCond %{REQUEST_URI} !projectname/(storage)$ [NC]
RewriteCond %{REQUEST_URI} !\.(html?|png|woff|ttf|eot|svg|woff2|jpg|gif|xml|rss|png|css|js|json)$ [NC]
RewriteRule ^(.*)$ /projectname/#/$1 [NE,R=301,L]
RewriteRule ^(.*)$ /projectname/abc/dist/$1 [L]
I want to load everything from projectname/abc/dist folder but not for the case when i have storage inside url then i want to load the data from storage folder only.
So as per rules defined here everything works fine but when i have storage inside url/src for image it still checks /projectname/abc/dist/storage/xxxx.png instead of checking /projectname/storage/xxxx.png as defined in htaccess rule.
I have tried using [S=5] skip with storage rule in htaccess does not works from reference http://httpd.apache.org/docs/2.4/rewrite/flags.html.
Also as per my understanding [L] is the last so it should stop the htaccess after storage rule but it does not.
I have tried implementing IF ELSE in htaccess but the examples i tried for IF ELSE does not help even.
reference https://blogs.apache.org/httpd/entry/new_in_httpd_2_4
reference http://httpd.apache.org/docs/2.4/rewrite/flags.html (check Skip IF ELSE stanza)
Any Idea would be useful.
You want to load from 'projectname/abc/dist'-folder EXCEPT when there is 'storage' anywhere in the requested URI?! If I got that right, that might help:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !storage
RewriteRule ^(.*)$ /projectname/abc/dist/$1 [END,P]
I added [P] for 'proxy' assuming that you don't want to show the customer he is rewritten by your server. If you don't mind seeing them, just leave that out.
First of all, L|last doesn't mean stop all processing.
If you are using RewriteRule in either .htaccess files or in sections, it is important to have some understanding of how the rules are processed. The simplified form of this is that once the rules have been processed, the rewritten request is handed back to the URL parsing engine to do what it may with it.
See also Ruleset Processing for how this works.
To load from /project/abc/dist, except when there's storage in the URL, you must first check for storage
RewriteCond %{REQUEST_URI} /storage/
RewriteRule /([^/]*$ /project/storage/$1 [L]
And then
RewriteRule ^(.*)$ /project/abc/dist/$1 [L]
Finally, to prevent a rewrite loop, prefix the rules with
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Everything together
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} /storage/
RewriteRule /([^/]*$ /project/storage/$1 [L]
RewriteRule ^(.*)$ /project/abc/dist/$1 [L]

Multiple RewriteRule to one RewriteCond

On the side I have about 400,000 subdomains. in view of the above, some calls also operate subdomain level, e.g..
subdomain1.example.com/some_action/
Such actions that should be performed only from the domain have 27.
Htaccess file I created a rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com
RewriteRule ^some_action1/?$ index.php?some_action1 [L]
If i add this line
RewriteRule ^some_action2/?$ index.php?some_action2 [L]
not work in the same way as for some_action3, etc.
I have added for each action separately
RewriteCond %{HTTP_HOST} ^(www.)?example.com
Can you somehow skip to harmonize?
Each RewriteCond condition only applies to the immediately following RewriteRule. That means if you have a bunch of rules, you have to duplicate the conditions. If you really don't want to do that for some reason, you could use a negate at the very beginning of all your rules. This may or may not be a good solution since it could affect how you make future changes to the rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www.)?example.com
RewriteRule ^ - [L]
RewriteRule ^some_action1/?$ index.php?some_action1 [L]
RewriteRule ^some_action2/?$ index.php?some_action2 [L]
RewriteRule ^some_action3/?$ index.php?some_action3 [L]
etc...
So the first rule checks for the negative of the host being example.com, and skips everything. Then you can add all your rules without having to worry about that condition.
However, if your "some_action" is always going to be part of the GET parameters, you can maybe just use a single rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [L]

Check URL and stop processing using htaccess

I want to check URL using htaccess. Developer might want run special file - specialfile.php. I use htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /specialfile\.php$
RewriteRule .* [L] #don't change adress
RewriteRule ^$ public/index.html [NC,L]
RewriteRule (.*) public/$1 [NC,L]
My idea was: if rewriteCond %{REQUEST_URI} !/specialfile.php$ true than htaccess should use RewriteRule .* [L] - that should mean that specialfile.php will be run and this all. But it doesn't work because it runs next rule: RewriteRule (.*) public/$1 [NC,L].
I think you are using the RewriteCond not correctly. The conditions only affect the next RewriteRule that follows.
Check out the example on the Apache Homepage. Since your 2nd RewriteRule is evalutated, I think your conditions are not correct. To get a litte bit more information about the rewriting, you should increase the log level. This is also documented here.
Your 2nd rule ^$ matches only an empty request btw. That's why it probably does not work as you expect it to.

mod_rewrite seems to ignore [L] flag

I'm trying to use the [L] flag in RewriteRule, but it doesn't seem to work. I'd like that if you call the page:
www.domain.com/admin/
it redirects you to:
www.domain.com/backend.php
Otherwise, if you call any other page (except for some pages) it redirects to:
www.domain.com/index.php
Here is what I have so far:
RewriteEngine on
RewriteRule ^admin/(.*) /backend.php/$1 [L]
RewriteCond $1 !^(index\.php|admin|assets|images|uploads|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
If I use only the first rule, it works (but obviously doesn't redirect other pages to index.php). If I add the second rule, the server seems to ignore the first rule (I think it is "overwritten" by the second rule, even if I added the [L] flag)
This isn't how L works. The rewrite engine will continually loop through all the rules until the URI going into the engine is the same as the one coming out. The L just tells the rewrite engine to stop applying rules in the current loop iteration. So say you have:
RewriteRule ^(.*)$ /foo/$1 [L]
RewriteRule ^(.*)$ /bar/$1 [L]
after 1 iteration, given the URI /blah, I get /foo/blah because it stops rewriting after the first rule (but will still continue to loop). If I remove the L:
RewriteRule ^(.*)$ /foo/$1
RewriteRule ^(.*)$ /bar/$1
after 1 iteration, given the URI /blah, I get /bar/foo/blah. Both rules get applied, one after the other because the L isn't there to stop it.
You need to add a condition in your second rule to prevent it from rewriting the first, either one of these will do:
RewriteCond $1 !^(backend\.php|index\.php|admin|assets|images|uploads|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
or:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond $1 !^(index\.php|admin|assets|images|uploads|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Your second rule is strange. I do not know what you are attempting to do by putting $1 as the value you are checking your condition against. It should probably look like this:
RewriteEngine on
RewriteRule ^admin/(.*) /backend.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !^(index\.php|admin|assets|images|uploads|robots\.txt|backend\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
Note I have also added a pass-through for backend.php

Mod rewrite - rewrite everything but

I have this line of code:
RewriteRule ^(.*)/$ allcities.php?state=$1 [QSA,L]
Howver how can I change it so it doesn't rewrite a new directory I have created, such as "admin". As currently that is being rewritten as well.
You can use RewriteCond for that:
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)/$ allcities.php?state=$1 [QSA,L]
Note the exclamation mark before the regex on the RewriteCond. It makes the regex revert its meaning. So in this case, the condition is true, if the request URI does not start with /admin.
Please see the mod_rewrite documentation for more information.
Rewrite rules are processed in the order that they are received. You will need to have a handler that will route to the existing directory before that rule is processed. For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ allcities.php?state=$1 [QSA,L]