apache rewrite, how to ignore subdirectories and pass to handler - apache

in virtual host environment, wish to use a handler. for specified directories, eg, images, script, want Apache to handle it. for all other subdirectories, wish to pass to handler.
everything is working except the last requirement
NOTE: THE FOLLOWING CODE HAS BEEN CORRECTED AND IS WORKING
<VirtualHost 127.0.1.12:80>
DocumentRoot /var/www
RewriteEngine On
ServerName monitor.phiddler.nit:80
UseCanonicalName Off
# for robots
RewriteCond %{HTTP_HOST} ^.
RewriteRule \.(php|php3|cgi|asp|aspx|jsp|cf|java|do|inc)$ /var/www [N,L]
# regular subdirs
RewriteCond %{HTTP_HOST} monitor.phiddler.nit
RewriteRule ^/(css|image|script|admin|ckeditor)/(.*) /data/phiddler/http/$1/$2 [L]
# ico file is in image
RewriteCond %{HTTP_HOST} monitor.phiddler.nit
RewriteRule \.ico$ /data/phiddler/http/image/$1.ico [L]
# name beginning with "." rerouted to ajax handler
RewriteCond %{HTTP_HOST} monitor.phiddler.nit
RewriteRule /\. /data/phiddler/http/ajax.php [L,QSA]
# all else goes to handler
RewriteCond %{HTTP_HOST} monitor.phiddler.nit
RewriteRule . /data/phiddler/http/handler.php [L]
</VirtualHost>
this should be easy, but somehow I'm missing it
also, is the RewriteCond necessary before each RewriteRule?

have fixed it in the question itself in order to serve as a correct reference
the way the question was asked and code layout was not conducive to good answers

Related

Redirect https://www.website.com to https://website.com not working

I make following post on redirection of https://www.website.com to https://website.com :
Issue with Let's Encrypt certificate : https://www.website.com not working with redirection to https://website.com
I can't get to achieve this redirection and I don't understand what is the reason.
If I type https://www.website.com, it remains on https://www.website.com and doesn't perform the redirection to https://website.com.
My config is a little special with a Zope server working with Apache2.
For the moment, here below are my rewrite rules (http://www.website.com and http//website.com are both redirected fine to https://website.com) :
<VirtualHost *:443>
# REWRITE to get https://www.website.com to https://website.com except for cgi-bin scripts
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/cgi-bin/search [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/awstats [NC]
RewriteRule ^/(.*) https://localhost:8443/++vh++https:%{SERVER_NAME}:443/++/$1 [P,L]
</VirtualHost>
<VirtualHost *:80>
<IfModule mod_rewrite.c>
# www to non www for HTTP and HTTPS
RewriteCond %{REQUEST_URI} ^/www\. [NC,OR]
RewriteCond %{REQUEST_URI} !^/podcast [NC]
# Rewrite below works : redirect 80 => https
RewriteRule ^/(.*) https://website.com/$1 [R=301,L]
RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]
</IfModule>
</VirtualHost>
What could be wrong here?
I believe there are few things of note:
One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met.
The RewriteRule directive......can occur more than once, with each instance defining a single rewrite rule. The order in which these rules are defined is important - this is the order in which they will be applied at run-time.
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
You also make use of L|last flags, which makes the engine stop processing further rules after RewriteRule is run.
The above sorta gives you an idea how the engine runs your rewrites:
RewriteRules are processed sequentially (unless you specify L flag, which you do for all rules)
Each RewriteRule can have many RewriteCond which all must match before RewriteRule is considered. It also means that RewriteCond must be repeated for each RewriteRule individually (there's however an interesting technique to group RewriteRules into if-then-else blocks)
In your case (VirtualHost context), only the URL-paths are matched by default unless you manually match HTTP_HOST variable.
With this in mind, I see a few issues with your rewrite rules (I swapped http and https vhosts around for readability but it does not matter):
<VirtualHost *:80> <!-- your http:// requests will end up with this block, because 80 is the port for http -->
<IfModule mod_rewrite.c>
# www to non www for HTTP and HTTPS <!-- I believe HTTPS traffic is NOT handled by this vhost at all, it arrives straight to *:443 queue -->
RewriteCond %{REQUEST_URI} ^/www\. [NC,OR] <!-- you evaluate path component of your request uri to begin with "www." (i.e. /www.index.html) - this will obviously never match, you however have OR flag, which proceeds to the second condition -->
RewriteCond %{REQUEST_URI} !^/podcast [NC] <!-- you check if the path does not start with "/podcast" - this is easy to test - try http://www.website.com/podcast and see if you get redirected to HTTPS - I suspect you will not -->
# Rewrite below works : redirect 80 => https <!-- I suspect it works by accident, please test it out with http://www.website.com/podcast to confirm my theory -->
RewriteRule ^/(.*) https://website.com/$1 [R=301,L] <!-- we end up here, and regardless of the requested path we issue a 301 redirect to https version of the website. This is marked as Last rule, so the engine should stop processing here -->
RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L] <!-- this I believe kicks in when you request a "/podcast" path - this will proxy the request to your http://localhost:9674/ -->
</IfModule>
</VirtualHost>
<VirtualHost *:443><!-- this is where your 301 redirect will com after bouncing through first set of rules above -->
# REWRITE to get https://www.website.com to https://website.com except for cgi-bin scripts
RewriteEngine On <!-- this is important, keep it on -->
RewriteCond %{REQUEST_URI} !^/cgi-bin/search [NC] <!-- you check whether url path does not contain /cgi-bin/search -->
RewriteCond %{REQUEST_URI} !^/cgi-bin/awstats [NC]<!-- AND does not contain /cgi-bin/awstats-->
RewriteRule ^/(.*) https://localhost:8443/++vh++https:%{SERVER_NAME}:443/++/$1 [P,L]<!-- if both conditions above are met - proxy the request to backend and stop further processing. -->
</VirtualHost>
As far as I see - there's no rule to rewrite https://www.website.com -> https://website.com, the only bit your https rewrite is checking is /cgi-bin
My suggestion would be along the following lines (it's probably not a copy and paste solution, but hopefully you will have gotten the gist):
<VirtualHost *:443>
RewriteEngine On
# www to non www for HTTPS
<!-- checking for the same thing again -->
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^/(.*) https://website.com/$1 [R=301,L] <!-- some people might argue second redirect here is excessive since you already arrived at correct host, but I'd leave this for you to sort out -->
<!-- your /cgi-bin checks can be merged into one regex -->
RewriteCond %{REQUEST_URI} !^/cgi-bin/(search|awstats) [NC]
RewriteRule ^/(.*) https://localhost:8443/++vh++https:%{SERVER_NAME}:443/++/$1 [P,L]
</VirtualHost>
<VirtualHost *:80>
<IfModule mod_rewrite.c>
# www to non www for HTTP
<!-- if you want to keep your `/podcast` on http check it first -->
RewriteCond %{REQUEST_URI} !^/podcast [NC]
RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]
<!-- everything else will get redirected to https -->
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^/(.*) https://website.com/$1 [R=301,L]
</IfModule>
</VirtualHost>
I had the same problem, for only one website of many that were virtually hosted on a amazon lightsail bitnami implementation.
After commenting out the line
ServerName mywebsite.com:80
In the /opt/bitnami/apache2/conf/httpd.conf file the problem was sovled.

apache htaccess rewrite with alias

We are changing our domain name and this is meant to work for stand alone applications. In Apache virtual host file the DocumentRoot is /var/www/website/html, not /var/www/example/html as in this block:
Alias /apps/dept /var/www/example/html/apps/dept
<Directory "/var/www/example/html/apps/dept/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I put an .htaccess file in /var/www/example/html/apps/dept directory as follows:
RewriteEngine On
RewriteBase /apps/dept/
RewriteCond %{HTTP_HOST} ^example.orgname.state.tx.us/apps/dept [NC]
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]
This seems to follow what is recommended here, http://httpd.apache.org/docs/current/mod/mod_rewrite.html and Apache : How to Use Rewrite Engine Inside Alias. I see no results. The new domain has a virutal host config in the VH file, also. This same basic rewrite works for our Drupal website which does not use an alias. What changes might be necessary to have the domain name rewritten with an appended application pathname? Is the RewriteBase incorrect?
Thx.
So you only want to redirect /apps/dept/, correct? This should work. Place it as an .htaccess or in the Apache config for example.orgname.state.tx.us and all should work as expected.
RewriteEngine on
RewriteRule ^/apps/dept/(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
So now, any requests going to this URL
http://example.orgname.state.tx.us/apps/dept/
Will now go to this URL:
http://example.orgname.texas.gov/apps/dept/
And any request parameters to the right of the URL will be passed along as well.
EDIT Just reread what you wrote here:
I put an .htaccess file in /var/www/example/html/apps/dept directory
as follows.
The .htaccess I described above should be placed in /var/www/example/html/ and not in the /apps/dept subdirectory.
But if you want the same behavior from an .htaccess placed in /apps/dept then use this:
RewriteEngine on
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
This way any request made from /apps/dept will to to example.orgname.texas.gov/apps/dept/ including subdirectories of /apps/dept such as /apps/dept/test_app1, /apps/dept/test_app2 or /apps/dept/test_app3.
Or perhaps try this:
RewriteEngine on
RewriteRule (.*)$ http://example.orgname.texas.gov/apps/dept/$1 [NC,L,R=301]
Note I removed the ^ which would force the RewriteRule to match the beginning of the URL.
You cannot match Request URI in %{HTTP_HOST} variable, it matches only domain name. Chang your rule to:
RewriteEngine On
RewriteBase /apps/dept/
RewriteCond %{HTTP_HOST} ^example\.orgname\.state\.tx\.us$ [NC]
RewriteRule ^(.*)$ http://example.orgname.texas.gov/apps/dept/$1 [L,R=301]

htaccess rewriterule to send a file to another file in the same directory

I have a directory that contains an index.php and an index.html file, both being published from a CMS. There's a specific IP address that will attempt to access index.html, but should instead be shown index.php in the same directory. All other traffic should act as normal.
I've been working with some variations of this code:
RewriteEngine On
RewriteCond %{REMOTE_HOST} ^123\.456\.789\.10
RewriteRule ^index\.(htm|html?)$ index.php [NC,R=301,L]
This does do the redirect, but of course it goes to the root of the site rather than staying in the same directory. It's somewhat unclear what the directory path will be in all cases, so I'd like to tell Apache to stay in the same directory it's in.
Is this possible?
Thanks,
Jonathan
If you want to have this rule for IP: 123.456.789.10 then you shouldn't have this IP with
negation like: RewriteCond %{REMOTE_HOST} !
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subdir/
RewriteCond %{REMOTE_HOST} ^123\.456\.789\.10$
RewriteRule ^index\.html?$ index.php [NC,R=302,L]

Redirect wildcard subdomains to subdirectory, without changing URL in address bar

I've read a lot of questions and answers about this on here but none that seem to solve my specific problem.
I want to redirect any subdomain to the subdirectory to match.
So: x.domain.com would go to domain.com/x, and y.domain.com would go to domain.com/y - But I want to do this without the URL in the address bar changing.
Here's what I have so far:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ /%1 [P,L]
But this takes me to a website redirect loop, with an incorrect address in the URL bar where the subdomain still exists.
For example, x.domain.com takes me to x.domain.com/x and I get a redirect loop error.
I'd be grateful if anyone can point me in the right direction! Nothing I change seems to work...
First of all, make sure that the vhost in the apache configuration is properly configured and all subdomains of domain.com are in the same host configuration (wildcard):
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
...
You can get the redirect working with the following htaccess configuration:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [L,NC,QSA]
Now, if you open asd.domain.com it should redirect you to domain.com/asd.
You will still have the problem, that the redirect is visible in the URL address bar. In order to prevent this, enable mod_proxy (and load the submodules) on your server and exchange the "L" flag with the "P" flag:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [P,NC,QSA]
If this doesn't work, viewing the vhost configuration and the content of error.log on subdomain calling will be helpful!
References:
.htaccess rewrite subdomain to directory
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p
This can be achieved in .htaccess without mod_proxy provided your server is configured to allow wildcard subdomains. (I achieved that in JustHost by creating a subomain manually named *). Add this to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
I named the subdirectories under $_SERVER['DOCUMENT_ROOT'] match with my subdomains like so:
/
var/
www/
html/
.htaccess
subdomain1.domain.com/
subdomain2.domain.com/
subdomain3.domain.com/
Where /var/www/html stand as 'DOCUMENT_ROOT'. Then put following code in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/
RewriteRule (.*) /%{HTTP_HOST}/$1 [L]
It works as redirect wildcard subdomains to subdirectories, without changing URL in address bar.
Beside of vhost, you may also put the subdirectories outside root and access it using alias as described here. Then put the same .htaccess code in that location.

conditional DirectoryIndex based on IP address, using .htaccess

I've got this in httpd.conf:
<VirtualHost xx.xxx.xx.xxx>
Options All +ExecCGI
ServerAdmin hostmaster#thehost.com
DocumentRoot /var/www/html/domain.com
ServerName dl.domain.org
DirectoryIndex dlindex1.html
</VirtualHost>
... which is fine (what I need as the DirectoryIndex for our 'dl.domain.org' subdomain), but now I also need to alter that DirectoryIndex based on IP address, using .htaccess.
Is this possible?
Other SO posts are telling me that I cannot set DirectoryIndex conditionally.. but instead have to use a RewriteRule.
If that is true, OK, but what RewriteCond and RewriteRule?
I am pretty noob in Apache, but anyway have tried many things, including (where the actual IPs are those of our 2 devs):
RewriteCond %{REMOTE_ADDR} ^111\.222\.333\.444$ [OR]
RewriteCond %{REMOTE_ADDR} ^555\.666\.777\.888$
RewriteCond %{SERVER_NAME} ^dl.domain.org
RewriteRule ^(.*)/$ $1/dlindex2.html
..or even just (as an absolute test):
RewriteCond %{REMOTE_ADDR} ^555\.666\.777\.888$
RewriteRule (.*)/dlindex1.html$ $1/dlindex2.html
But it seems that whatever I try it just serves up the DirectoryIndex dlindex1.html as per httpd.conf, as opposed to the dlindex2.html I want served up as the default page in that subdomain when a devs IP is calling.
Can any one point me to what I can do to get what I am after? i.e. this: ...to actually, or even just effectively, alter DirectoryIndex based on IP address, using .htaccess, on the fly?
2 ways turned out to work for me:
https://unix.stackexchange.com/questions/44129/conditional-directoryindex-based-on-ip-address-using-htaccess
https://webmasters.stackexchange.com/questions/32727/conditional-directoryindex-based-on-ip-address-using-htaccess
e.g.:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^12\.34\.56\.78$
RewriteCond %{REQUEST_URI} index\.html$
RewriteRule .* /index1.html
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78$
RewriteCond %{REQUEST_URI} index\.html$
RewriteRule .* /index2.html