Domain masking across different servers with Htaccess - apache

I have a Wordpress install on domain1.com/blog but ideally I would like it to display as sub.domain2.com/blog.
These domains are on separate servers. Ideally I would install WP on same server as domain2 but this isn't possible right now.
I've been able to get sub.domain2.com/blog to display the WP homepage but is it possible for htaccess and apache to rewrite the urls on domain1.com to display as sub.domain2.com?
Other than simple url rewrites I have limited experience with htaccess but I suspect for security reasons this isn't possible.

You can of course, redirect your requests on domain1.com to sub.domain2.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(blog/.*)$ http://sub.domain2.com/$1 [R=301,L]
Put the above in a htaccess file for the domain1.com.

Related

How to redirect www.mydom.com to mydom.com

I have a dedicated server with WHM/cPanel and hosting multiple domains on Linux.
For one of my domains, I'd like to get all http://www.mydom.com traffic to automatically go to http://mydom.com
This needs to happen for all subdirectories, files etc. e.g. http://www.mydom.com/aFile.php should go to http://mydom.com/aFile.php
Other subdomains such as img.mydom.com should still go to img.mydom.com and not be redirected.
How can I accomplish this? Thx.
You can setup redirection code in your .htaccess file to redirect your all traffic from www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [nocase]
RewriteRule ^(.*) http://example.com/$1 [last,redirect=301]

Redirect for path but only for specific domain

We have different domains that are hosted on our server. Recently one of the main sections of our site has been moved to another server and been given a subdomain:
http://www.mysite.com/store
Has been moved to
http://store.mysite.com
Within our apache VirtualHost we wanted to redirect all traffic from the old domain to the new one:
Redirect permanent /store http://store.mysite.com
The problem is, we have other hosted sites that are being redirected now:
http://www.othersite.com/store
http://api.greatsite.com/store
We don't want this. How can I only have apache do redirects if http://www.mysite.com/store which has the /store path, and ignore other domains with /store?
Use mod_rewrite based code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^/?store(/.*|)$ http://store.mysite.com [L,R=301,NC]

How to redirect from one port to another?

I've a domain on which I'm running two different web applications at different ports.
A Java web application on http://example:8888/foo
A PHP website on http://example:8080/bar
The requirement is, if an user tries to access the root of http://example:8888 or http://example:8080, then the user should be redirected to http://example:8888/foo.
How can I achieve this requirement?
mod_rewrite can achieve this easily, you will be using 301 redirects
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^/$ http://example.com:8888/foo [R=301,L]
Make sure this is added in the vhost config section of both sites.

Redirecting PDF links from another domain using htaccess

We have two domains, let's call them first.com and second.com
We have a directory in second.com called reports, where all our PDFs are located, but we would like to these same PDFs accessible from first.com as well.
Can we redirect let's say first.com/reports/84839049.pdf to second.com/reports/84839049.pdf using htaccess?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain1.com/$1 [R=301, L]
Yes.
redirect /requested/url http://second.com/result/url
http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirect
You may want to consider using mod_rewrite though, unless you asked for an .htaccess configuration specifically because you have no access to the server configuration and mod_rewrite is disabled or not loaded.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://webdesign.about.com/od/mod_rewrite/qt/site_redirects.htm
You'll need some grasp of regex for mod_rewrite, but it can make configuration of the redirects a lot faster than having to add a redirect for every file on your site(s).

How can I redirect all traffic from one domain to another with an .htaccess file?

Say I have a subdomain xxx.yyy.com running Apache. The files are stored in /home/someone/public_html/xxx.
What I want to do is redirect all requests to a domain name zzz.com which is using the same location for its files. (In other words, xxx.yyy.com and zzz.com are aliases for each other)
I just want people accessing zzz.com, so if someone goes to xxx.yyy.com they should be redirected to zzz.com.
Can this easily be done with a rewrite rule in an .htaccess file?
Try
RewriteCond %{HTTP_HOST} ^xxx\.yyy\.com
RewriteRule (.*) http://zzz.com/$1 [R=permanent,QSA,L]