Simulating a 2-level If-Else using RewriteCond - apache

I'm trying to get my head around RewriteCond, and want to rewrite any requests either to a static html page (if it exists), or to a specific index.php (so long as the requested file doesn't exist).
To illustrate the logic:
if HTTP_HOST is '(www\.)?mydomain.com'
if file exists: "/default/static/{REQUEST_URI}.html", then
rewrite .* to /default/static/{REQUEST_URI}.html
else if file exists: {REQUEST_FILENAME}, then
do not rewrite
else
rewrite .* to /default/index.php
I don't seem to have much trouble doing it when I don't need to test for the HTTP_HOST. Ultimately, this one .htaccess file will be handling requests for several domains.
I know I could get around this with vhosts, but I'd like to figure out how to do it this way.
I'm not too familiar with some of the other flags, will any of them be of use here (like chain|C, next|N or skip|S)?
Thanks in advance!
UPDATE: I've managed to do it, but would appreciate alternatives:
RewriteCond %{HTTP_HOST} ^(domainA|domainB)\..* [NC]
RewriteCond %{DOCUMENT_ROOT}/%1/static/%{REQUEST_URI}.html -f
RewriteRule (.*)? /%1/static/$1.html [NC,L]
RewriteCond %{HTTP_HOST} ^(domainA|domainB)\..* [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /%1/index.php [L,QSA]
UPDATE #2: With help from Gumbo's answer, came up with another. I like that this would would require less maintenance in the case of added domains. (Thanks Gumbo!)
Are there any reasons why I shouldn't set ENV variables?
RewriteCond %{HTTP_HOST} ^(domainA|domainB)\..*$ [NC]
RewriteRule ^ - [E=APP:%1]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:APP}/static/%{REQUEST_URI}.html -f
RewriteRule (.*)? /%{ENV:APP}/static/$1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /%{ENV:APP}/index.php [L,QSA]

I would probably do it like this:
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteRule ^ - [S=2]
RewriteCond %{DOCUMENT_ROOT}/default/static%{REQUEST_URI}.html -f
RewriteRule !^default/static/ default/static%{REQUEST_URI}.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^default/static/ default/index.php [L]
This is similar to your updated example. Except that the first rule will skip the following two rules if the host is not appropriate. And the RewriteRule patterns exclude any path that starts with /default/static/. But your rules are already pretty good.

Related

Remove php extension and parameters using Mod Rewrite in .htaccess to create two seo friendly urls

I have read ALOT of threads about htaccess rewriting but struggling to get anything to work as I need it to.
I am spinning my wheels here for a few hours.
I have two pages in my folder.
/proposal/index.php
/proposal/dl.php
So normal queries would look like this.
/proposal/?code=123abc
/proposal/dl.php?code=123abc.
But I want to send it to the user like this:
/proposal/123abc/
/proposal/dl/123abc/
Can someone help me with this?
TIA
NOTE
I did try the code below which works perfectly on index.php but has no effect on dl.php (if I add dl.php to the mix)
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
This probably is the variant you are looking for. Both rules capture the actual argument embedded in the requested URL to re-use it in the internally rewritten request.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [END]
RewriteRule ^dl/([^/]+)/?$ dl.php?code=$1 [END]
RewriteRule ^([^/]+)/?$ index.php?code=$1 [END]
Yo may also want to add redirection rules to redirect clients using the "old" URLs:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [END]
RewriteCOnd %{QUERY_STRING} ^code=([^/]+)$
RewriteRule ^dl\.php$ dl/%1 [R=301,END]
RewriteRule ^index\.php$ %1 [R=301,END]
RewriteRule ^$ /%1 [R=301,END]
RewriteRule ^dl/([^/]+)/?$ dl.php?code=$1 [END]
RewriteRule ^([^/]+)/?$ index.php?code=$1 [END]
For this it is a good idea to start out with a 302 redirection and only to change it to 301 when everything works as expected.
Both variants use relative paths so that the rules work in distributed configuration files somewhere deeper than the document root of your http server. I personally prefer to implement such rules in the real host configuration instead of using distributed configuration files ("htaccess") for various reasons.
In the end, this worked.
RewriteEngine On
RewriteRule ^dl/(.*) dl.php/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
Now I am able to call the pages like this:
/proposal/123abc/
/proposal/dl/123abc/
All I did was add the line below to my original code.
RewriteRule ^dl/(.*) dl.php/$1 [L]
The structure of my subdirectory is as follows:
/proposal/.htaccess
/proposal/index.php
/proposal/dl.php
Now I am able to provide nice URLs to the clients for these two files and call up their content using my parameter like:
/proposal/123abc
/proposal/dl/123abc
Which would be like this in a normal way
/proposal/index.php?code=123abc or /proposal/?code=123abc
/proposal/dl.php?code=123abc

How to add/ignore a subfolder in htaccess file?

I have build an app at http://url/sdf19/
I have a .htaccess placed in /sdf19/ containing RewriteRule for clean urls.
But I have built a PDF generating tool, which is in a subfolder /inc/tools.
I need to link to it direct to run before headers.
Despite a few hours of searching, trying snippets, generators, etc. I cannot get any request to http://url/sdf19/inc/tools to be allowed, without the existing RewriteRule set taking over
Here is my starting file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)\/?$ index.php?page=$1 [NC]
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ index.php?page=$1&id=$2 [NC]
RewriteRule ^([a-z]+)\/([a-z]+)\/?$ index.php?page=$1&action=$2 [NC]
RewriteRule ^([a-z]+)\/([a-z]+)\/([0-9]+)\/?$ index.php?page=$1&action=$2&id=$3 [NC]
I've tried to add this on line 4;
RewriteCond %{REQUEST_URI} !^ inc/tools/ [NC]
This gave RewriteCond: bad argument line error
I've tried to add this to line 2;
RewriteCond %{REQUEST_URI} ^/inc/tools/(.*)$
RewriteRule ^.*$ - [L]
Desired result is that I can allow direct access to http://url/sdf19/inc/tools > everything I have tried so far i get redirected to base http://url/sdf19/
Maybe, we could step by step solve this problem. First step, we might just want to make something work with as less as boundaries that'd be possible.
Let's design a low boundary expression, maybe something similar to:
(.+)(\/inc\/tools)
From here, we can just add \/inc\/tools to the RewriteCond, just for testing. Later, we can modify that.
RegEx
If this wasn't a desired expression, you can modify/change your expressions in regex101.com.
RegEx Circuit
You can also visualize your expressions in jex.im:
RewriteRule Test
You can test your RewriteRules in htaccess.madewithlove.be.
I'm not so sure about the rest of RewriteRules, but I'm assuming they are working fine and not conflicting with the new one. Maybe, this or something similar would work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} \/inc\/tools [NC]
RewriteRule ^(.*)$ $1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)\/?$ index.php?page=$1 [NC]
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ index.php?page=$1&id=$2 [NC]
RewriteRule ^([a-z]+)\/([a-z]+)\/?$ index.php?page=$1&action=$2 [NC]
RewriteRule ^([a-z]+)\/([a-z]+)\/([0-9]+)\/?$ index.php?page=$1&action=$2&id=$3 [NC]
</IfModule>

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]

flexibility in .htaccess for mod_rewrite rules

i would like to redirect my old php files to new seo friendly ones:
user.php?user=$var1&task=$var2 -> url/$var1/$var2
There are 2 problems. $var2 is not set every time, so i do not know how to deal that and the querystring is always added at the end.
I use the following redirect rule for testing (without $var2)
RewriteCond %{THE_REQUEST} ^(.+)user\.php(.+)$
RewriteCond %{QUERY_STRING} user=([^/]+)$
RewriteRule ^(.+)\.php$ %1 [R]
I get this:
url/$var1/?user=$var1
Second problem is the rewrite rule so that url/$var1 -> user.php?user=$var1
Without it i get a server error.
In the moment i tried this static one for testing, but this is not the only rule so that the Condition is wrong here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ user.php?user=$1 [L]
How i get the correct results and more flexibility with the variables?
mod_rewrite is not my world in the moment, so i hope you can bring light in the dark.
Thx ruven
1) The way to prevent the query string at the end there is to add a ? at the end of the URL you rewrite to.
RewriteCond %{THE_REQUEST} ^GET\ /user\.php
RewriteCond %{QUERY_STRING} user=([^&=]+)$
RewriteRule ^user\.php %1? [R]
And in case both var1 and var2 are set it would be
RewriteCond %{THE_REQUEST} ^GET\ /user\.php
RewriteCond %{QUERY_STRING} user=([^&=]+)$
RewriteCond %{QUERY_STRING} task=([^&=]+)$
RewriteRule ^user\.php %1/%2? [R]
Combine these (the second one first) and it should redirect as needed
2) Since this is a kind of 'catch all' URL you should put this as the last option in your .htaccess and redirect everything that is not a file or a directory to user.php and then let user.php figure out if the user exists, and if not respond with HTTP 404.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ user.php?user=$1 [L]
(I've removed the / at the end as it's not a good idea to have two URLs for the exact same content).

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.