conditional DirectoryIndex based on IP address, using .htaccess - apache

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

Related

Stop virtualhost rewrite rules executing if a rule was matched in the global config

I have the following setup.
Apache running with a separate VirtualHost file for each site.
Each of these has their own set of rewrite rules, for http to https for example which is all running fine.
What we would like to happen is this, from the global config we need to be able to check if a request is for a particular subdirectory. If it is then we should allow this request to process as it should but at that point we do not want the individual virtual host file rewrite rules to kick in. Therefore allowing this directory to be served on non https connections and not be redirected to https.
I have set up the rewrite rules and can match on the directories and redirect to an external url if it matches from the global which shows its inheriting but if I try to just allow it through the virtual hosts rewrites kick in and it redirects.
I have tried using L and END but this did not work either.
Is there any way of achieving this without editing the virtual host files that are already configured?
Main httpd config entry
<Directory "/www">
Options Indexes Includes FollowSymLinks MultiViews
AllowOverride All
allow from all
Order allow,deny
Require all granted
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/sub_directory/$ [NC]
RewriteRule ^(.*) $1 [L,END]
#RewriteRule ^(.*) - [L,END]
#RewriteRule ^(.*) http://www.google.com [L,END] # This does get triggered
</Directory>
sample virtual host file.
<VirtualHost *:80>
ServerName urlone.com
ServerAlias urltwo.com
DocumentRoot /www/
RewriteEngine On
# redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://urlone.com$1 [R=301,L]
</VirtualHost>
so if I visit urlone.com it should redirect to https://urlone.com but if I visit urlone.com/sub_directory it needs to not allow the redirect to https.
I hope this makes sense to someone and thanks in advance for any help.
In global httpd.conf:
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/sub_directory$ [NC]
RewriteRule ^ - [E=PATH_MATCHED:true]
(if needed, you can add additional rules, or additional flags to the above rule)
In virtual_host.conf
RewriteCond %{ENV:PATH_MATCHED} !=true
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://urlone.com%{REQUEST_URI} [R=301,L]

Apache mod_rewrite not happening on localhost

I can't seem to get my head around this. I just pulled down my website files from my server to work on them offline. However, my rewrite conditions are no longer working as expected.
I've been googleing for the last 3 hours and keep coming to these solutions:
put garbage in the .htaccess file to make sure it's being read. I did that and got a 500 error so it is.
Make sure mod_rewrite is enabled, and I make sure it was listed in php_info(). That's not the problem.
Other than that, I can't figure this thing out.
Here's my .htaccess. All I want to do is remove index.php from my URLs:
# Rewrite url no index.php
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule . index.php
Here's my virtual host config at the moment:
<VirtualHost *:80>
ServerAdmin xxxx#gmail.com
DocumentRoot "c:/wamp/www/myapp/public_html/"
<Directory "c:/wamp/www/myapp/public_html/">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ServerName myapp.local
ErrorLog "logs/myapp.local"
CustomLog "logs/myapp.local" common
</VirtualHost>
I'd also like to make it known that rewrite rules seem to work a bit when they're in the virtual host config. So something like this:
<VirtualHost *:80>
ServerAdmin xxx#gmail.com
DocumentRoot "c:/wamp/www/myapp/public_html/"
<Directory "c:/wamp/www/myapp/public_html/">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
ServerName myapp.local
ErrorLog "logs/myapp.local"
CustomLog "logs/myapp.local" common
</VirtualHost>
I had other problems with this though since I have nested applications (one's at / and the other at /app2/). I also seemed to have problems with the !-f condition being ignored and it would rewrite the URLs of my images and css.
Does anyone have any ideas on how to fix this? Thanks in advance!
You should use your code like this to remove index.php from the URL.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\ [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L,QSA]
And then in your head section of your html add this to fix CSS problem.
<base href="http://myapp.local" />

Apache: Don't apply RewriteRule if requested host is a IP address

How doesn't Apache apply an RewriteRule if the requests isn't the normal domain name but instead the server IP?
Current rules (to make domain just accessible without the www):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
I tried already to add RewriteCond %{HTTP_HOST} !^123.321.123.321$ [OR] but it seemed not to work.
If you have access to the server config file, this works for me (setting it in httpd.conf):
<Location />
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^123.123.123.123$
RewriteRule (.*) http://www.mainsite.example.com/ [L]
</Location>
If I visit the site by IP address, it isn't rewritten. If I use (eg.) http://mainsite.example.com, the address is rewritten (to include the www.). The [L] (when used in httpd.conf) stops rewrite processing there (instead of now trying to rewrite the new address).
In there you can also have
ServerName www.mainsite.example.com
ServerAlias mainsite.example.com
ServerAlias www.mainsite
ServerAlias mainsite
ServerAlias 123.123.123.123
so that people can use any of those addresses (the 2nd and 3rd aliases let internal users omit the domain), instead of rewriting to force or add www.

apache rewrite, how to ignore subdirectories and pass to handler

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

Apache alias from sub domain to domains article

I wonder is it possible to make an alias from sub domain, let's say http://sub.domain.com, to http://domain.com/some-article. When user types in address bar sub.domain.com, he should see that article(address in address bar should be the same sub domain, so I think redirect its not what I need). Any ideas?
Your help would be appreciated.
The Apache ServerAlias only covers a hostname and not a fully qualified URI.
In the above example you should use mod_rewrite, making sure to omit the R flag since you don't want to redirect i.e provide a 30X status code in the response.
So in the above example you could have the following
<VirtualHost *:80>
...
ServerName domain.com
ServerAlias sub.domain.com
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^$ /some-article [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^(.*) http:/domain.com/some-article$1 [L]
...
</VirtualHost>