Using .htaccess to redirect by domain, either in the URL or in HTTP_HOST - apache

I am hosting several domains on the same apache server and I want to set them all up so that /home is unique to each domain. (suppose I have 3 domains example.com example1.com and example2.com) I have setup my file structure like this...
/www/htdocs/domain/example
/www/htdocs/domain/example1
/www/htdocs/domain/example2
http://example.com/home/ ==> .../domain/example/
http://example1.com/home/ ==> .../domain/example1/
http://example2.com/home/ ==> .../domain/example2/
So I have an htaccess rule like this
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$ [NC]
RewriteRule ^home/(.*)$ /domain/%1/$1 [L]
This works perfectly. But I want to extend it to include www.example.com So I add this rule.
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)\.(.+)$ [NC]
RewriteRule ^home/(.*)$ /domain/%2/$1 [L]
That works great, and is generally how I have been building my .htaccess file. It is growing quite unweildy and I need to rethink my approach. So I am trying to concatenate the above two rules into a single block. Here is what I have but it isn't working...
RewriteCond ^home/
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$ [OR]
RewriteCond %{HTTP_HOST} ^.+\.(.+)\.(.+)$ [OR]
RewriteRule ^home/(.*)$ /domain/%2/$1 [L]
Additionally and separately I would like a rule that would cause the following URL to return error 404.
http://example1.com/domain/example/logo.jpg

I am answering my own question. At least the first part. I am still looking for an answer to the second part.
RewriteCond %{REQUEST_URI} ^/home/
RewriteCond %{HTTP_HOST} ^.+\.(.+)\.(.+)$ [OR]
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$
RewriteRule ^home/(.*)$ /domain/%1/$1 [L]

Related

htaccess issue configuring pseudo style virtual host

I am having some issues with my htaccess file. I am trying to use rewrite rules to create a pseudo style virtual host on my apache shared hosting server.
I have 3 domains under one account and I want to achieve the following:
If not rule exists then go to the root (domain1.co.uk)
If domain2.co.uk set the directory to d1
If domain3.co.uk set the directory to d2
Irrespective of domain, if the www. is missing, add it.
The file I have at the moment is as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain2.co.uk$ [OR]
RewriteRule ^(.*)$ /d2/$1
RewriteCond %{HTTP_HOST} ^domain3.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain3.co.uk$ [OR]
RewriteRule ^(.*)$ /d3/$1
This originally looked like it was working except all domains seem to go into the first rule and direct to /d2. The result is that if domain1.co.uk, domain2.co.uk or domain3.co.uk is entered then they all go to domain1.co.uk.
Any thoughts?
You put an [OR] on each of the second rules. It instructs Apache to look into the next rule if this one fails. When both rules fail, it then goes to the next one, which is non-existent. So your RewriteRule gets triggered in any case. That would be equivalent to doing IF something OR somethingElse OR, which is nonsensical.
Remove the [OR] from your second rule and it should work.
I would explicitly use the first domain name in rule one and you need to tell the first rule to ignore the other 2 domains. There is also no need for [OR] just to check for www. You can make that one condition.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^(www\.)?domain(2|3)\.co\.uk$
RewriteRule ^(.*)$ http://www.domain1.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.co\.uk$
RewriteRule ^(.*)$ /d2/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain3\.co\.uk$
RewriteRule ^(.*)$ /d3/$1 [L]

htaccess wildcard redirect not working

I have an htaccess wildcard redirect on a website, it redirect another domain to a main one and also redirects www to none www with wildcard for the main domain. The following rules are made with cpanel, however it does not use the wildcard.
These same rules work fine on many other sites just not the one in question.
RewriteCond %{HTTP_HOST} ^www\.domain1\.ca$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
Any ideas? I am out of ideas for what could be causing this.
To add more clarification:
if i go to www.domain1.ca/some-page it should redirect to domain1.ca/some-page
However instead it goes to domain1.ca.
Tested in every browser, remade the rules, removed all cache and did multiple dns flush's, nothing has changed this.
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^ http://domain1.ca%{REQUEST_URI} [R=301,L,NE]

Need a htaccess redirect rule to ignore a certain folder

I'm redirecting my .com website to .net using this
RewriteCond %{HTTP_HOST} ^cartoonizemypet\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.cartoonizemypet\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.cartoonizemypet\.net\/$1" [R=302,L]
Which works (huzzah!) The problem is I need to also exclude a single folder and all of it's contents: http://www.cartoonizemypet.com/catoonizer
I've been messing around with it all day, trying to adapt other peoples from here, but I just keep breaking the site. I'm afraid I just don't know that much about rewrite rules.
Does anyone know how I can make it work?
Try:
RewriteCond %{REQUEST_URI} !^/catoonizer
RewriteCond %{HTTP_HOST} ^cartoonizemypet\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.cartoonizemypet\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.cartoonizemypet\.net\/$1" [R=302,L]
Another way of doing it:
# Turn the engine on if it's not already
RewriteEngine On
# Check to make sure we're on the right host (www is optional)
RewriteCond %{HTTP_HOST} ^(www\.)?cartoonizemypet\.com$ [NC]
# Check to make sure we're not at the catoonizer URI (I assume that's a misspelling
# but it's what was in the example).
RewriteCond %{REQUEST_URI} !^(/catoonizer) [NC]
# If all conditions are met, redirect as 302.
RewriteRule ^(.*)$ http://www.cartoonizemypet.net/$1 [R=302,L]

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]

mod_rewrite for specific domains in a mappings file

I have a bunch of domains that I want to go to one domain but various parts of that domain.
# this is what I currently have
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*\.?foo\.com$ [NC]
RewriteRule ^.*$ ${domainmappings:www.foo.com} [L,R=301]
# rewrite map file
www.foo.com www.domain.com/domain/foo.com.php
www.bar.com www.domain.com/domain/bar.com.php
www.baz.com www.domain.com/other/baz.php.foo
The problem is that I don't want to have to have each domain be part of the RewriteCond. I tried
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R=301,L]
but that will do it for EVERY domain. I only want the domains that are in the mappings file to redirect, and then continue on to other rewrites if it doesn't match any domains in the mappings file.
I'm close as I figured out the the case matching, but unable to figure out the www prefix. If I use the first one below, it works without www. If I use the second one, it works with the www. If I use BOTH - neither work.
RewriteCond %{HTTP_HOST} (.*)$ [NC] # works for without www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] # works with www
RewriteCond ${domainmappings:%1|NOTFOUND} ^(.+)$ [NC]
RewriteCond %1 !^NOTFOUND$
RewriteRule ^.*$ ${domainmappings:%1} [L,R=301]
Any ideas?
You are on the right track. What you have to do is use the pipe operator on the map so that you have a catch-all.
RewriteCond ${domainmappings:%{HTTP_HOST}|NOTFOUND} ^(.+)$
RewriteCond %1 !^NOTFOUND$
RewriteRule ^.*$ ${domainmappings:%1} [L,R=301]
The second condition will not match if the host is not in the list. You still have to deal with the www prefix, and the case matching, but you get the idea.