conditional DirectoryIndex in .htaccess - apache

Is it possible to make the DirectoryIndex value in a .htaccess file conditional based on IP, so that - for example - my IP see's DirectoryIndex as index.html and everyone else sees DirectoryIndex as index.php?
Is there a solution other than mod_rewrite?

As far as I know, there is no conditional for DirectoryIndex. You could simulate that with a mod_rewrite directive like this one:
RewriteCond %{REMOTE_ADDR} your_ip
RewriteCond -d
RewriteRule (.*)/$ $1/index.html
If you want to exclude other visitors of the site from viewing index.html then also use
RewriteCond %{REMOTE_ADDR} !your_ip
RewriteRule (.*)/index.html$ $1/index.php

Using the provided information I beleive the following is what you need:
RewriteCond %{REMOTE_ADDR} ^your_ip$
RewriteRule (.*)/$ $1/index.php
RewriteCond %{REMOTE_ADDR} !^your_ip$
RewriteRule index.php$ index.html
So that only your IP can see index.php and everybody else will see index.html
or possibly:
DirectoryIndex index.html
RewriteCond %{REMOTE_ADDR} ^your\.ip\.000\.000$
RewriteRule ^index.html$ index.php

Related

In .htaccess to prioritize DirectoryIndex to a RewriteRule

I want files to be accessed when they are available as specified in the DirectoryIndex line. E.g. when someone types in www.mywebsite.com/mydir/ it will redirect correctly. For this I have in my .htaccess the line:
DirectoryIndex index.html default.php
When a filename or directory does not exist I want it to redirect to a file (target_handler.php) which is located in the same directory as my .htaccess file. For this I have placed several lines after the DirectoryIndex:
RewriteEngine On
RewriteCond %{ENV:URI} ^$
RewriteRule ^(.*)$ - [ENV=URI:$1]
RewriteCond %{ENV:BASE} ^$
RewriteCond %{ENV:URI}::%{REQUEST_URI} ^(.*)::(.*?)\1$
RewriteRule ^ - [ENV=BASE:%2]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}target_handler.php [L,QSA]
What I want is for Apache to first check to see if there is an index.html or default.php available, and only upon unavailability perform a rewrite. Currently the behaviour seems random(?), so if someone types www.mywebsite.com/mydir/ then Apache sometimes does the rewrite first without checking if there is an index.html in the specified folder.
Help appreciated
PS: I am testing using: xampp
Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.15
You can omit DirectoryIndex and do it all using mod_rewrite itself:
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}index\.html -f
RewriteRule ^ index.html [L]
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}default\.php -f
RewriteRule ^ default.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^target_handler\.php$ target_handler.php [L,NC]

Apache Rewrite Directory Index

Basically, if the user agent string contains something then I want DirectoryIndex to be ignored. This doesn't appear to work at all.
You can maybe try something like this:
DirectoryIndex index.html index.php
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !Kodi [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ /_h5ai/server/php/index.php [L,QSA]

Change document root using .htaccess on wamp

I have scratching my head over it for a long time now. Can't manage to get it to work. (I am a noob with apache that can be one reason also). Ok here is the problem in nutshell. I am using wamp and I have a directory Retailer. There is another directory inside it which is called public that contains the index and otherfiles. I want to make this public directory document root. I want to achieve this with .htaccess
My Rewrite module for apache is turned on.
Here is what I have tried:
RewriteEngine on
RewriteBase /public/
RewriteRule ^index.php$ test.php
And also I have tried
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost/Retailer$ [NC,OR]
RewriteCond %{HTTP_HOST} ^localhost/Retailer$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
And I have tried
RewriteEngine on
RewriteCond %{HTTP_HOST} ^http://localhost/Retailer/$ [NC,OR]
RewriteCond %{HTTP_HOST} ^http://localhost/Retailer/$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
But result in all these cases is the same. That is:
Any help will be appreciated
Ahmar
Use this rule in your Retailer/.htaccess file:
RewriteEngine on
RewriteBase /Retailer/
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
You should config this not in .htaccess file but in apache config file httpd.conf:
<VirtualHost 127.0.0.1:80>
DocumentRoot "/path/to/project/Retailer/public"
ServerName "retailer.local"
ServerAlias "www.retailer.local"
</VirtualHost>
Also you need to update your hosts file with the next line:
127.0.0.1 retailer.local
And restart your web server!

Apache not showing index.html on address bar

I would like to show my index.html file on address bar. When you go to http://www.example.com/ to redirect it to http://www.example.com/index.html and show that index.html on address bar.
EDIT
This is my .htaccess file. I have try neo example but with no luck.
DirectoryIndex naslovnica.php
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* 404.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /naslovnica\.php
RewriteRule ^naslovnica\.php$ http://example.com/naslovnica.php [R=301,L]
You will have to look into Apache DocumentRoot Directory. Have a look at the following Apache Docs...
Apache DocumentRoot
You will have to create the index.html and put it in the DocumentRoot directory.
From the DOCS...
If your DocumentRoot "/usr/web" then an access to http://my.example.com/index.html refers to /usr/web/index.html.
Edited
The following document explains about hiding the index. Guess you can use it to unhhide the index as well.
Mod_Rewrite – Hide index.php
The following might be a start towards finding the right solution...
Options +FollowSymLinks
RewriteEngine on
# Redirect client requests for www.example.com/ to "www.example.com/index.html" in main doamin
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html
RewriteRule ^index\.html$ http://www.example.com/index.html [R=301,L]

Change the DirectoryIndex based on a domain/sub-domain in .htaccess

I have a shared hosting with one domain and one sub-domain (for mobile and clients). Each domain and sub-domains has different default index pages. The hosting company told me to put everything in my .htaccess file since I don't have access to the httpd.conf.
What I want to do is this:
If a user goes to domain1.com the DirectoryIndex should be: index.html
If a user goes to mobile.domain1.com the DirectoryIndex should be: mobile-index.html
If a user goes to post.domain1.com the DirectoryIndex should be: post.php
If a user goes to vote.domain1.com the DirectoryIndex should be: vote.php
Edit:
In addition, if I go to domain1.com/page/ the DirectoryIndex should be: index.html. If I go to mobile.domain1.com/page/ the DirectoryIndex should be: mobile-index.html
What can I put in my .htaccess file in order to change the DirectoryIndex for each sub-domain?
Thank You very mich
<IfDefine> does not work like that. <IfDefine> only runs when apache starts. You should go with a mod_rewrite solution. view #tzakrajs answer.
You can use this in your .htaccess file:
SetEnvIf Host ^www\. page=www
SetEnvIf Host ^mobile\. page=mobile
rewriterule ^.*$ test.php?subdomain=%{ENV:page} [QSA,L]
Simply just configure all your sub-domain using SetEnvIf and then simply let the PHP do its magic.
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^.*/$ index.html [R=302,L]
RewriteCond %{HTTP_HOST} ^mobile.domain1.com$
RewriteRule ^.*/$ mobile-index.html [R=302,L]
RewriteCond %{HTTP_HOST} ^post.domain1.com$
RewriteRule ^.*/$ post.php [R=302,L]
RewriteCond %{HTTP_HOST} ^vote.domain1.com$
RewriteRule ^.*/$ vote.php [R=302,L]
You can set using just oyur .htaccess file like this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule DirectoryIndex index.html
RewriteCond %{HTTP_HOST} ^mobile.domain.com$ [NC]
RewriteRule DirectoryIndex mobile-index.html
...