Generlized IP Canonicalization Solution Within .htaccess - apache

I am looking for a generalized IP canonicalization solution that would not hard code IP address in my htaccess file. I generally run my applications on amazon EC2 instances and IP addresses frequently change. I'd hate to have to manually update htaccess every time it does so. So specifically, I am looking for htaccess rules that would match pages accessed via ip address and perform a 301 redirect to the actual domain.

Had you considered using some variety of Dynamic DNS instead? That would let the IP addresses change frequently while maintaining the same DNS names.

I’m going to assume you don’t have any subdomains. The following should do the trick:
Options +FollowSymLinks
RewriteEngine on
# 1
RewriteCond %{HTTP_HOST} .
# 2
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
# 3
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This will redirect requests for anything1 other than the actual domain2 for all resources3.
EDIT: For subdomains, then the following should work:
# If it's one of the domains of the IP address …
RewriteCond %{HTTP_HOST} ^domain\.com [OR]
RewriteCond %{HTTP_HOST} ^111\.222\.222\.111
# … then redirect request for all resources.
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Related

Subdomains that don't exist are indexed by Google - Zombie subdomain removal via .htaccess

I'm experiencing an unusual issue where Google is indexing subdomains of my site that don't exist. It looks like these 'subdomains' are created from misspellings such as w. ww.. There's even some instances of http://abc.www.example.com being indexed.
My main concern here is with content duplication in the SERPS as there are several of these non existent / zombie subdomains indexed by Google. I want these subdomains to either drop out of the SERPS or be redirected to the secure www. version of the website.
I'm experienced in web development, but server configuration and .htaccess / mod Rewrite rules are not my biggest strength.
There's currently a rewrite rule to force https across the site - could this be amended to force the zombie subdomains to redirect to https://www.? If it is possible, can I ensure it doesn't effect real subdomains of the site?
Current .htaccess redirect :
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,R=301,L]
To address the comments... You can't block hostnames (subdomains) in robots.txt if all subdomains are pointing to the same place (the same site), unless you internally rewrite those requests to a different robots-block.txt file. However, robots.txt doesn't necessarily prevent indexing, it prevents crawling, and if these subdomains are already indexed, then they are going to remain indexed for while if you simply block crawling.
Are you on a shared server? Or do you have your own? This will only be a "default host" issue if this is your own server and your site is the first (or only) host defined on it. (This is rare to be a shared server issue.) Otherwise, this sounds like you have a "wildcard" subdomain defined in DNS (and corresponding ServerAlias in the server config).
Remove the wildcard subdomain and it will resolve your issue.
Otherwise you can redirect (or block) any non-canonical hosts in .htaccess. For example:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteRule ^ - [F]
The above would send a 403 Forbidden for any request that is not for example.com or www.example.com.
Aside:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,R=301,L]
This is not correct. It only canonicalises http://example.com. It won't canonicalise/redirect http://www.example.com or https://example.com since the two conditions are implicitly AND'd. You need to OR the two conditions:
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{SERVER_PORT} 80
In order to match example.com or http.

Redirection between domains

I have two webs on two different domains. One is on domain.com and the other web is on domain.es.
I am shutting down the web on domain.es but first I have to redirect every request to domain.es to domain.com. This is easy and I had already done it with RewriteCond & RewriteRule, like this:
www.domain.es/ -- redirects to --> www.domain.com/
But I am having a problem now. I also need to redirect one specific page from domain.es to domain.com like this:
www.domain.es/a_page/ -- redirects to --> www.domain.com/another_page/
I am having problems when redirecting with both rules, as when apache detects a request to www.domain.es/a_page it goes to www.domain.com/ and not to the specific page I need.
I have this in my virtualhost:
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
RewriteRule ^(.*)& http://www.domain.com/another_page/ [R=301,L]
RewriteCond %{HTTP_HOST} ^domain.es/
RewriteRule ^(.*)& http://www.domain.com/ [R=301,L]
I've got the [L] flag so apache would stop processing rules, but this is not working.
Any help would be appreciated.
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
The line above is never going to match anything - you're asking if the host is equal to domain.es/a_page/, which is a host as well as a path. You need to test against only the hostname, and then redirect based on the path, e.g.
RewriteCond %{HTTP_HOST} ^domain\.es
RewriteRule ^a_page/ http://www.domain.com/another_page/ [R=301,L]

redirect url from Domain A to another but applications exist on Domain A only

I have requirement on OHS server(apache) to redirect all requests from domain A to domain B but the pages needs to be rendered from domain A only. Below configuration doesn't seems to work.
RewriteCond %{HTTP_HOST} ^http://portal.mycompany.com$
RewriteRule ^(.*)$ http://newportal.mycompany.com:85/$1 [R=301,L]
Am i doing anything wrong?
For domain redirects could you simply point your new domain B to A using either CNAME or A records in the domain registrar?
There are several issues. You can't do a 301 redirect and expect to stay on Domain A. One way you might be able to get this to work is the use of mod_proxy and the P flag. Also the http_host variable only matches the hostname.
You need to make sure mod_proxy is enabled and you can try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^portal.mycompany.com$
RewriteRule ^(.*)$ http://newportal.mycompany.com:85/$1 [P]
In this way your domain A is sending a redirect to user browser, so the page is going to be provided from domain B.
If you have to make domain A providing pages from domain B, but user do not have to see domain B, you may check Proxy and/or ProxyPass rule from mod_proxy.
One more thing, if you proxy domain B thru domain A, you may have to rewrite inpage links, so take a look at ProxyHTMLEnable and ProxyHTMLURLMap
Dot (.) is a special character in the RewriteCond pattern, Escape it using a backslash
Try this
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.portal\.mycompany\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ http://newportal.mycompany.com:85/$1 [R=301,QSA,NC,L]

Forward multiple domain names to multiple sites

Have searched and viewed many threads, but mostly are about forwarding multiple domains to 1 single site/domain.
What i have now is around 50 domainnames which i have directed to my server ip address.
On my server i use Directadmin.
What i would like is to have the most efficient/easiest way to manage these domainnames and have them forward to (different) sites.
So for example,
Domain1.com, needs to be forwarded to someotherdomain1.com
Domain2.com, needs to be forwarded to someotherdomain2.com
Domain3.com, needs to be forwarded to someotherdomain3.com
What would be the easiest way for me to set something like this up?
Thank you in advance
You can use these type of rules:
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain1\.com$ [NC]
RewriteRule ^ http://someotherdomain1.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain2\.com$ [NC]
RewriteRule ^ http://someotherdomain2.com%{REQUEST_URI} [R=301,L,NE]
Alternatively if you have access to Apache config then you can utilize RewriteMap feature and have just one single rule here like this:
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.com$ [NC]
RewriteRule ^ http://${domainMap:%1}.com%{REQUEST_URI} [R=301,L,NE]
In your Apache config you need to define domainMap as:
RewriteMap domainMap txt://path/to/domain-map.txt

Apache (htaccess) redirect to directory based on user selected domain

I found some interesting thoughts here on stackoverflow, but none is stated clearly enough for me, so I have to ask myself if it even could be done :)
Situation:
I have linux with apache http server installed and a bunch of sites in folders in www directory with different contents.
Let's say:
www/Site1 (Phpbb)
www/Site2 (Wordpress)
www/Site3 (Own web pages)
I own one domain intended for use on Site3 (let's call it site3.com, www.site3.com).
I have created two more domains at no-ip.org (let's call them site1.sytes.net and site2.sytes.net).
All three created domains are pointed to port 80 of linux (apache) servers ip address.
So we have these domains ready:
www.site3.com
site1.sytes.net
site2.sytes.net
Now the question:
How to create redirects to particular directories for users who want content from those domains.
I want user who want to get "site1.sytes.net" (and wrote that to browser) to be pointed to Site1 directory and rewrite browser address to omit that directory and put just "http://site1.sytes.net".
For example:
User want: site1.sytes.net [/index.html]
Server goes to: www/Site1 [/index.html]
User see in browser: http://site1.sytes.net [/index.html]
And so on:
site2.sytes.net [/index.html] => www/Site2 [/index.html]
www.site3.com [/index.html] => www/Site3 [/index.html]
If I am not mistaken HTTP_HOST will return just domain, so it is useless for trying to find out what user wrote to browser for site1.sytes.net and site2.sytes.net (because it will return just sytes.net).
The %{HTTP_HOST} is indeed what you want, and yes, it only matches the domain. But you use it in a condition and you use either %{REQUEST_URI} or the rule itself to match against the URI. Try putting this in your htaccess file in your document root (which I assume is the same for all your sites, otherwise, there's no point in doing any of this):
RewriteEngine On
RewriteCond %{HTTP_HOST} site1.sytes.net [NC]
RewriteCond %{REQUEST_URI} !^/Site1
RewriteRule ^(.*)$ /Site1/$1 [L]
RewriteCond %{HTTP_HOST} site2.sytes.net [NC]
RewriteCond %{REQUEST_URI} !^/Site2
RewriteRule ^(.*)$ /Site2/$1 [L]
RewriteCond %{HTTP_HOST} www.site3.com [NC]
RewriteCond %{REQUEST_URI} !^/Site3
RewriteRule ^(.*)$ /Site3/$1 [L]