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

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
...

Related

htaccess redirect to new domain with parameters

I need to redirect from abc.com/param to test.bcd.com/?query=param via htaccess.
Is it correct?
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?test\.bcd\.com$ [NC]
RewriteRule .* http://test.bcd.com/?query=%{REQUEST_URI} [R=301,L]
Please try this
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^query=param [NC]
RewriteCond %{HTTP_HOST} !^(www\.)?test\.bcd\.com$ [NC]
RewriteRule ^ http://test.bcd.com%{REQUEST_URI}?%{QUERY_STRING} [R=301,L]
for redirect to index.html then try this
Please add following two lines to allow a specific file to load just in case there is no default index file setup already
Options -Indexes
DirectoryIndex index.html index.php
Note : if you have to load first php index file then you have set like
DirectoryIndex index.php index.html

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]

htaccess redirect from domain1 to domain2

I know, this should be easy, but I cannot make it work.
I have 2 domains pointing to the same folder in an Apache server, bahiadivers.com and bahiadivers.cl.
I want to redirect bahiadivers.cl to bahiadivers.cl/es/ and change the address bar.
I try this (among a million things)
rewritecond %{HTTP_HOST} ^bahiadivers.cl [nc]
rewriterule ^/$ http://www.bahiadivers.cl/es/$1 [r=301,nc]
but didn't work... I mean, the URL is not changing in the browser address bar, but also the languge (/es/) is not working... how should I do this?
Thanks!
Try this for your .htaccess
# Apache configuration file
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# If URL is www.example.com/es/Blah, use /Blah/
# Prevents needing to change existing links.
RewriteBase /es/
# Rewrite www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www.bahiadivers.cl
RewriteCond %{HTTP_HOST} ^bahiadivers.cl
# Rewrite any inbound URL Bits to http://bahiadivers.cl/es/...
RewriteRule (.*) http://bahiadivers.cl/es/$1 [R=301,L]
</IfModule>
That should do it.

conditional DirectoryIndex in .htaccess

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