How to exclude certain sections of a site from Mod ReWrite Rules - mod-rewrite

I am moving most pages from a site and I want to redirect all requests to a 404 error page except all/any pages in a certain cname on the site, all/any pages in a certain sub-directory on the site and a certain page in a certain directory on the site.
Here is what I have so far:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^blogs.example.com [NC]
RewriteCond %{REQUEST_URI} !^/private [NC]
RewriteCond %{REQUEST_URI} !^/public/special-form.php [NC]
RewriteCond %{REQUEST_URI} !^/public/special-form_submitted.php [NC]
RewriteCond %{REQUEST_URI} !^/error404.php [NC]
RewriteRule ^(.*) http://example.com/error404.php [R=301,L]

Your issue is mainly the / at the start of your RewriteRule, why?
Because you're using it with an .htaccess so the RewriteRule path starts without the / at the begin.
If it was inside the VirtualHost then that would have worked just fine.
Basically this is what your rule is telling the server to do after our comments conversation and your update:
Options +FollowSymlinks
RewriteEngine On
# if domain is not blogs.example.com
RewriteCond %{HTTP_HOST} !^blogs.example.com [NC]
# and if address does not start with /private
RewriteCond %{REQUEST_URI} !^/private [NC]
# and if the file is not /public/special-form.php
RewriteCond %{REQUEST_URI} !^/public/special-form.php [NC]
# and if the file is not /public/special-form_submitted.php
RewriteCond %{REQUEST_URI} !^/public/special-form_submitted.php [NC]
# and if the file is not /error404.php
RewriteCond %{REQUEST_URI} !^/error404.php [NC]
# then redirect to http://example.com/error404.php
RewriteRule ^ http://example.com/error404.php [R=301,L]
So anything else including images or css or not listed on the conditions would be redirected, in order to avoid that you would have to verify all the needed files within those 2 php pages and white list it, for example with a condition like this:
RewriteCond %{REQUEST_URI} !^/public/[^.]+\.(css|pdf|jpg|doc)$ [NC]
Basically the above means anything within the public folder that ends with .css or .pdf or .doc or .jpg should be displayed.
NOTE: Keep in mind that if your images and documents and whatever else you need to show to your users from those 2 PHP pages are not within the public folder the above will not work, that was merely an example to illustrate an easy way to couple multiple types of files into it.
You could also resume this 2 conditions:
RewriteCond %{REQUEST_URI} !^/public/special-form.php [NC]
RewriteCond %{REQUEST_URI} !^/public/special-form_submitted.php [NC]
as follow:
RewriteCond %{REQUEST_URI} !^/public/(special-form_submitted|special-form)\.php [NC]
You would end with:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^blogs.example.com [NC]
RewriteCond %{REQUEST_URI} !^/private [NC]
RewriteCond %{REQUEST_URI} !^/public/(special-form_submitted|special-form)\.php [NC]
RewriteCond %{REQUEST_URI} !^/public/[^.]+\.(css|pdf|jpg|doc)$ [NC]
RewriteCond %{REQUEST_URI} !^/error404.php [NC]
RewriteRule ^ http://example.com/error404.php [R=301,L]
Just a side information, since you're redirecting anything that does not match the conditions and you're not passing along anything to the redirect, you don't need the (.*), merely using ^ alone will do the job.

Related

Rewrite example.txt to another based on the domain

Im using a multi-install for my webpage with two different domains and i need for each domain a unique robots.txt
like
https://www.domain1.tdl/robots.txt should use the https://www.domain1.tdl/robots_domain1.txt
and
https://www.domain2.tdl/robots.txt should use the https://www.domain2.tdl/robots_domain2.txt
Check this rewrites in .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.tdl [NC]
RewriteCond %{REQUEST_URI} ^\/robots\.txt$
RewriteRule (.*) https://www.domain1.tdl/robots_domain1.txt [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.tdl [NC]
RewriteCond %{REQUEST_URI} ^\/robots\.txt$
RewriteRule (.*) https://www.domain2.tdl/robots_domain2.txt [L]

Mod Rewrite Maintenance page that uses multiple URLs

I have a site the uses multiple URLs for a single application. Depending on the URL of the site you will get different content. I need to create a rewrite rule that redirects a user to a different page depending on what URL the user hits.
For example:
If a user visits www.foo.bar the user will be redirected to www.foo.bar/maintWWW.html
But if the user visits www.bar.foo the user will be redirected to www.bar.foo/maintWWW2.html
Remember that since we are using the same application but different URLs that these 2 html pages needs to be named differently in order to serve different content.
I managed to use this but it only redirects both URLs to a single page,
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintWWW.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintWWW.html [R=302,L]
I tried replacing the %{REQUEST_URI} with the actual URL of the site I want redirected, but it did not work.
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond http://www.foo.bar !/maintWWW.html$ [NC]
RewriteCond http://www.foo.bar !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintWWW.html [R=302,L]
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond http://www.bar.foo !/maintWWW2.html$ [NC]
RewriteCond http://www.bar.foo !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintWWW2.html [R=302,L]
How exactly would I get this to work? Can I include multiple URLs to be redirected to the same page? It would be nice to account for development and staging URLs as well. Example below again:
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond http://www.foo.bar http://staging.foo.bar http://dev.foo.bar !/maintWWW.html$ [NC]
RewriteCond http://www.foo.bar !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintWWW.html [R=302,L]
You need to check HTTP_HOST in your condition to check for the domain and then redirect based on that.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.bar [NC]
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintWWW.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintWWW.html [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.)?bar\.foo [NC]
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintWWW2.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintWWW2.html [R=302,L]
If you have multiple hostnames or aliases that you can use in your setup you can do this.
RewriteCond %{HTTP_HOST} ^(www|dev|staging)\.foo\.bar [NC]
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintWWW.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintWWW.html [R=302,L]

Regard a couple of OR-RewriteCond directives if another RewriteCond evals

I have the following configuration,
RewriteCond %{HTTPS} !^on$ [nocase,ornext]
RewriteCond %{HTTP_HOST} !^www\. [nocase,ornext]
RewriteCond %{HTTP_HOST} !\.com$ [nocase]
RewriteRule ^(.*)$ https://www.acme.com/$1 [redirect=301,last]
the purpose is to redirect to a canonical URL if the request is either not HTTPS, nor begins with www. or ends with .com.
For being seamlessly compatible with developer engines, I want to exclude all these directives if %{HTTP_HOST} includes, for example, dev.internal or so. In this case the RewriteRule should be skipped immediately. Since the three ORs are evaluated with the higher precedence than an (implicit) AND, I wonder how and where to place my dev.internal exception...
Thanks for any advice!
//edit: hmm... if OR has the higher precendence, shouldn't
RewriteCond %{HTTP_HOST} !internal\. [nocase]
RewriteCond %{HTTP:X-Forwarded-Proto} !^https$ [nocase,ornext]
RewriteCond %{HTTP_HOST} !^www\. [nocase,ornext]
RewriteCond %{HTTP_HOST} !\.com$ [nocase]
RewriteRule ^(.*)$ https://www.acme.com/$1 [redirect=301,last]
work then?
If my understanding of ornext is correct then yes, your way should work (can someone else confirm it?).
Here's another way, if you don't want to rely on that:
RewriteCond %{HTTP_HOST} internal\. [nocase] # If it's an internal host...
RewriteRule .* - [skip=1] # ... skip the next rule (and leave the URL unchanged)
RewriteCond %{HTTP:X-Forwarded-Proto} !^https$ [nocase,ornext]
RewriteCond %{HTTP_HOST} !^www\. [nocase,ornext]
RewriteCond %{HTTP_HOST} !\.com$ [nocase]
RewriteRule ^(.*)$ https://www.acme.com/$1 [redirect=301,last]

RewriteCond HTTP_HOST still passes

I'm building a preprod environment where the client and developpers will be able to check that everything is working fine for their website they are about to put up but i must disable all HTTPS redirection because at this point it would fall in the PROD site wathever the HOSTNAME is sent in the request.
This is one of the examples i'm using:
RewriteCond %{HTTP_HOST} (www\.)?domain\.com
RewriteCond %{HTTP_HOST} ^s\.domain\.com
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/?basket/$
RewriteCond %{REQUEST_URI} !^/?basket/add/
RewriteCond %{REQUEST_URI} !^/?basket/delete/
RewriteCond %{REQUEST_URI} !^/?basket/modify/
RewriteCond %{REQUEST_URI} !^/?basket//add/
RewriteCond %{REQUEST_URI} !^/?basket//delete/
RewriteCond %{REQUEST_URI} !^/?basket//modify/
RewriteCond %{REQUEST_URI} !^/?basket/info_panier.js
RewriteRule ^/?basket/.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Problem is, even if i visit:
http://s.domain.com/basket//coordonnees/login/
It still redirects me to the HTTPS version, but i have a rewrite condition on the s.domain.com saying to skip this rule... What am i doing wrong?
CrazyCoder
Never mind i found it two seconds later, but this might help other people:
The condition:
RewriteCond %{HTTP_HOST} (www\.)?domain\.com
Came clashing on the condition:
RewriteCond %{HTTP_HOST} ^s\.domain\.com
Because i didn't put a ! off the start. If i put ! before the (www.)?domain.com it will not match s.domain.com and thus work fine...

RewriteCond to exclude a directory and it's sub-directories

I've been trying to get a wikipedia style language thing to work. So that the url will be en.example.com for English, fr.example.com for French, etc... This is working fine however I would like the admin area to always default to base language, i.e. not set the LANGUAGE environment variable. I've tried adding RewriteCond %{REQUEST_URI} !^admin [NC] but it seems to have no effect.
My mod_rewrite code is as follows:
# Handle languages
# Picks up the language code from the browser accept-language parameter
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2}).*$ [NC]
RewriteCond %{HTTP_HOST} !^[a-z]{2}\.[a-z]{2,}\. [NC]
RewriteRule ^(.*)$ http://%1.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_URI} !^admin [NC]
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.[a-z]{2,}\. [NC]
RewriteRule ^(.*)$ - [ENV=LANGUAGE:%1,QSA]
# Redirecting all requests to one script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\+a-zA-Z0-9,%\(\)\_\ -/]+)$ /index.php [NC,L,QSA]
Thanks for any help, I'm sure it's something really stupid that is wrong, as usual.
Your problem is that REQUEST_URI doesn't start with admin, its going to have a slash in front... it might start with /admin if you have no rewrite base... so you can change it to !^/admin or just !admin or !admin/ if all your code is in foo.com/admin/*
REQUEST_URI is going to be the entire GET like "/foo/bar.html"

Resources