Redirect for path but only for specific domain - apache

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]

Related

Apache virtual hosting url redirect from one domain to other domain without showing the url in the browser

Apache virtual hosting url redirect from one domain to other domain without showing the url in the browser?
That's not redirect, that's called Reverse Proxy.
Example:
ProxyPass /url-path/ http://backend.example.com/url-path/
This will reverse proxy all requests to /url-path/whatever to the server backend specified
More information at:
http://httpd.apache.org/docs/2.4/mod/mod_proxy.html
http://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
No, there isn't a way to do this with .htaccess if your sites are on different servers. Doing so would present big security hole, imagine if someone does this with a bankĀ“s website.
However, if both are hosted on the same server try this on your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$ [OR]
RewriteCond %{HTTP_HOST} ^http://www.domain1.com$
RewriteRule (.*)$ http://www.domain2.com$1 [P]
</IfModule>
If you own both domains you could accomplish this with domain name forwarding. Check the options in your registrar (maybe godaddy, or dns managers like cloudflare).

Domain masking across different servers with Htaccess

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.

301 redirect with domain both pointing to same directory

I am currently moving from one domain to another. Both domains are add on domains in CPanel and point to the same sub-directory.
How can i set up 301 redirect that redirects all of the old domains pages to the same file in the new domain. For example olddomain.com/example to newdomain.com/example. Since both domains reside in the same sub directory the both utilize the same .htaccess file.
Since both domains share the same .Htaccess file I'm having trouble getting the 301 redirect to work.
Put the following in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^newdomain.com
RewriteRule /(.*)$ http://newdomain.com/$1 [R,L]
It will check what your host-header is, and will redirect anything to newdomain.com if that's not the current site.

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.

Redirect from one domain to another, without the user realizing it

I have a bunch of domains on on of my servers. I'd like to be able to redirect some domains to a different domain without the user knowing, so not a 301 redirect.
An example, redirect domain.com to masterdomain.com/sites/domain.com. So when visiting domain.com, the user would be displayed with the data on masterdomain.com/sites/domain.com.
Also masterdomain.com and domain.com are on the same server.
How could I do this using Apache? mod_rewritem mod_alias?
If both host names use the same document root, you can do this:
RewriteCond %{HTTP_HOST} =example.com
RewriteRule !^sites/example\.com(/|$) sites/example.com%{REQUEST_URI} [L]
This rule will prepend /sites/example.com if the requested URI path does not already starts with that.
GoDaddy calls this a domain Alais.
Here is a link where it is explained
http://webwizardworks.com/web-design/domain-alias.html