.htaccess intrupting add on domains on shared hostin - apache

This is the code in my .htaccess file which is in the root directory of my shared hosting.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9\-]+)?$ interest.php?interest=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9\_\-]+)?/$ interest.php?interest=$1 [QSA,L]
The problem is, In the same directory lies folders of my add on domains. When I am trying to access them, the 2nd line is creating problem and redirecting it to my primary domain. I want the .htaccess to effect only root folder and not its sub folders. I tried reading other similar questions here and tried changing my htaccess accordingly but the problem remained unresolved.
For example.
its redirecting
mysubdomain.mydomain.com
to
www.mydomain.com/mysubdomainfolder

Have your first rule like this:
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Then clear your browser cache and restart your browser.

Well, .htaccess files are designed to apply recursively.
You could try putting a .htaccess in each of the subfolders that correspond to your add on domains with RewriteEngine Off if you want no redirects, or an alternate rewrite rule if you need them.
In addition you may want to add another rewrite condition for the rule, say your add on domain is blah.com you could add
RewriteCond %{HTTP_HOST} !^(.*.)?blah.com
Edit: I should add the new rewrite condition should go below RewriteCond %{HTTP_HOST} !^www.
Edit2: Okay so subdomains then. In which case you basically need to add a Rewrite condition specifying not to apply the rewrite rule if it's a known subdomain. For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^(subdomain1|subdomain2|subdomain3)\.domain\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Now subdomain1.domain.com won't redirect (or subdomain2/3), www.domain.com won't redirect, but anything else will.

Related

.htaccess redirect issue with multiple .htaccess file

I am using .htaccess file like below for redirect www to non www on my public_html directory
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
its working fine and redirect like below
www.example.com
to
https://example.com
Now in my sub directory called latest, I have another .htaccess file like below
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
which is making my url clean like below
www.example.com/latest/index.php?page=1
to
www.example.com/latest/1
but even I have .htaccess file in home directory which is making www to non www, my this directory does not redirect www to non www.
I want redirect
www.example.com/latest/1
to
https://example.com/latest/1
I am sure .htaccess file in my directory is causing issue but I do not know how to resolve it. Let me know if anyone here can help me for the same.
Thanks!
The problem is, mod_rewrite is not inherited (at least by default). If you have a rewrite rule in the "default" directory .. You're going to have to include that rule in all subsequent directories "below" that. I have never seen (or heard of) a setting for allowing mod_rewrite to allow inheritance from parent .htaccess files. In general, this is just accepted as "the way it is".
UPDATE
THIS QUESTION In Server Fault explains this as well. The mod_rewrite rules from the previous htaccess files don't even get processed.
COMBINE THE 2 in the SUB DIRECTORIES
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# add this only to sub directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

Redirection through .htaccess not working

I have a domain which has sub-domains (add-on). I am trying to redirect users from main (old) domain to one of sub-domains (new).
First I tried to redirect everything that user types within the OLD domain except one page and one directory... Now this EXCEPT feature is going out of control. I have been trying and implementing all possible options but some it gives an error and stops redirection.
Here are the lines from .htaccess file which I am trying:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} oldrootdomain.com [NC]
RewriteRule ^/view_card.php(.*)$ http://newsubdomain.com/view_card.php$1
RewriteRule ^/m/(.*)$ http://oldrootdomain.com/m/$1
RewriteRule ^(.*)$ http://newsubdomain.com/ [R=301,L]
It seems you run into an inifinite loop.
You DON'T want to redirect for those exception. So try some additional RewriteConds
RewriteCond %{HTTP_HOST} oldrootdomain.com [NC]
RewriteCond %{REQUEST_URI} !view_card.php$
RewriteCond %{REQUEST_URI} !m/.*$
RewriteCond %{REQUEST_URI} !m$
RewriteRule ^(.*)$ http://newsubdomain.com/ [R=301,L]
Also you seem to have not understood the RewriteCond/RewriteRule thing entirely
Any successive stream of RewriteConds does only affect the directly following RewriteRule

Redirecting old domian to new domain by .htaccess

I am in process of migrating my old domain to new domain using Apache Mod-Rewrite moduled and its .htaccess file.
we have almost same structure of the new domain which includes
URL's
Database
except the domain name, like it was www.oldurl.com and now its like www.newurl.com and this is what i have in my .htaccess file of Old domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Above settings seems to be working fine except in one case, we have few URL's in my old domain which has either been removed or structure has been changed so in that case above rule will not work.i came to know about adding something like this in my .htaccess file beside what i have described above
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Redirect 301 /my-page http://www.newurl.com/your-page
i have total of 20+ such URL's and i am wondering do i need to map those all 20+ URL to there new URL's and will i need to take care about any order in which they should be put in the file.
i am also wondering how Apache will work, will it look at each mapped URL for any match? or it works in some other way?
The Redirect directive won't be bound to the RewriteCond conditions and will always redirect /my-page to http://www.newurl.com/your-page, also, mod_rewrite has precedence over mod_alias so the RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L] rule gets applied before the Redirect directive gets looked at. However, if the .htaccess file sits within the document root of both the oldurl.com and newurl.com domains, the Redirect directive will be applied after the browser gets redirected to http://www.newurl.com/my-page, thus redirecting (again) to http://www.newurl.com/your-page
So, it doesn't matter what order you have these in, since mod_rewrite gets applied first. If you have 20 URLs that need to redirect to new ones on your new site, you can enumerate them each in their own Redirect. Otherwise, if you'd rather not have the browser get redirected twice, you can enumerate them using the mod_rewrite engine:
RewriteEngine On
# redirect the changed URLs individually
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page$ http://www.newurl.com/your-page [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page2$ http://www.newurl.com/your-page2 [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page3$ http://www.newurl.com/your-page3 [R=301,L]
# Finally, redirect everything else as-is
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Note that order does matter here. Having to repeat the 2 Conditions for HTTP_HOST is kind of ugly, you can maybe get around that by using the SKIP, but it's probably better to just repeat them. But if you have access to your server config or vhost config, take a look at the RewriteMap Directive, which allows you to create a mapping of, in your case, old urls to new urls and you can reduce all the individual changed url rewrites to a single one:
Inside your server/vhost config, something like this:
RewriteMap newurls txt:/path/to/file/map.txt
Where the /path/to/file/map.txt will look something like:
my-page your-page
my-page2 your-page2
my-page3 your-page3
etc...
And your combined rules would look like:
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^(.*)$ http://www.newurl.com/${newurls:$1} [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]

How can I change all subdomains to read different content from their respective subdirectories?

I have my DNS configured to accept any subdomain (wildcard *), but I am having trouble feeding back the required content to the browsers.
I would like each subdomain to return the relative content, which resides in subdirectories of the same name within the public_html path of my server.
eg, example.domain.com/picture.jpg would actually request the file at public_html/example/picture.jpg
Currently I have tested the followed .htaccess code, but it is not functional:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ %1/$1 [L]
This code, and similar tests, can redirect based on the subdomain (%1) fine, but the request string ($1) seems to be the issue.
Maybe you could take a look at the mod_vhost_alias module :
http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html
Try the following:
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+).domain.com [NC]
RewriteRule ^/(.*) http://domain.com/%1/$1 [L]
I didn't test this one but I use similar rules for proxying:
RewriteCond %{HTTP_HOST} !^domain.local [NC]
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+).domain.com [NC]
RewriteRule ^/(.*) http://domain.local/%1/$1 [P]