Redirect domain1 to domain2 with path after slash - apache

I have 2 domain
domain1.com
domain2.net
i want redirect domain1.com to domain2.net with path alias after slash, like this
domain1.com/joomla(this is folder)/path-to-content >>> domain2.net/joomla(this is folder)/path-to-content
i used this rule but didn't work with path after slash
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ http://www.domain.net/persian/$1 [R=301,L]
This rule works like this
domain1.com/joomla(this is folder)/path-to-content >> domain2.net/joomla(this is folder)
my server is centos 6.2 and apache and content management system is Joomla.

You could also use a domain alias, or let the domains share the same docroot (if the content on both domains is to be identical) in your hosting control panel.

you said you want to REDIRECT to domain2.net. instead of the rewrite condition you could use a simple redirect to achieve an equal result. add this code to the .htaccess file in the public_html directory of domain1.com:
redirect / http://domain2.net/

Related

htaccess Redirect from folder to external URL

I'd like to setup a redirect from URL domain1.com/c/name1 to URL abc.domain2.com/ID1
In my root htaccess file I tried the following redirect statement, but unfortunately it is not working.
RewriteEngine on
RewriteRule ^/c/name1(/) https://abc.domain2.com/ID1 [R=301,L]
The problem might be, that I'm using a DNS setting to root the domain1.com to an external IP address, which is my landing page platform.
RewriteRule ^/c/name1(/) https://abc.domain2.com/ID1 [R=301,L]
The URL-path that the RewriteRule pattern matches against does not start with a slash. You are also enforcing the trailing slash, which is not present on your example URL.
Try the following instead:
RewriteRule ^c/name1/?$ https://abc.domain2.com/ID1 [R=301,L]
The /? makes the trailing slash optional.
The problem might be, that I'm using a DNS setting to root the domain1.com to an external IP address, which is my landing page platform.
I'm not sure what you mean by this, but the .htaccess file will need to be the root directory of wherever domain1.com resolves to.

Rewrite rule for subdomains

Is it possible to use .htaccess for a subdomain to point to a file on the main domain?
I have example.com as a main domain and created subdomains: one.example.com, two.example.com, etc through DirectAdmin. The subdomains should point to app.php on the main domain but in the url the subdomain should persist.
So navigating to one.example.com should be pointing to app.php which lives in the main domain example.com, but the url should still show one.example.com
I have looked and tried a lot of the rewrite rules found on .htaccess rewrite subdomain to directory and elsewhere.
Currently I have this rewrite rule in a .htaccess file in the subfolder of the subdomain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/app.php$1 [L,NC,QSA]
This redirects and changes the url to example.com/app.php while I would like the url to show the subdomain: one.example.com/whatever
Is it possible to do this with .htaccess ,or is there maybe even a better way to do this?
A RewriteRule with a target on another server will always do a redirect
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.
To map the URL http://example.com/app.php into your subdomains without redirecting, you might try the P|proxy flag
RewriteRule ^(.*)$ http://example.com/app.php/$1 [P,QSA]
Also keep the final note in mind
Note: mod_proxy must be enabled in order to use this flag.
Usually, this can be done by
a2enmod proxy proxy_http
If the target URL is https://..., you must also enable mod_proxy_connect, and activate SSL in your main or virtual host config with SSLProxyEngine
SSLProxyEngine on
Depending on your needs, ProxyPass might be a viable solution too.

How to add a rewrite rule for domain and it's subdomains in Plesk 11.0.9?

I need to redirect a request file from all possible subdomains to one file which is located in top level domain:
^.example.com/robots.txt >> http://example.com/robots.txt
The robots.txt may or may not exist in the subdomain httpdocs, this rule must be exucuted first (we want to block all crawlers in advance).
The folder structure provided by Plesk 11.0.9 is:
example.com/
...
httpdocs/
subdomains/
sub1/
httpdocs/
example.com is owned by root.
Where can I add an apache rewrite rule that would be trigger for all subdomains?
Edit: I tested with ping doesntexist.example.com and the request does get directed to example.com which means teoretically there should be a point in processing to execute the rewrite logic.
Try placing this rule in /example.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.(example\.com)$ [NC]
RewriteRule ^robots\.txt$ http://%1/$0 [L,R=302,NC]

DNS/.htaccess files to redirect subdomain to specific folder

I'm trying to redirect my subdomain to a flder. I think I have the .htaccess code from another post here. Something like:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^clients\.example\.com$
RewriteRule ^ http://example.com/clients/root/app%{REQUEST_URI} [L,P]
What DNS settings should I set up for the clients. subdomain to tie it in with the .htaccess.
You need to create a CNAME to point clients.example.com to the same server as example.com. Then you need to make sure your webserver is setup to serve the clients.example.com in the same root as example.com.
Of, you can simply setup clients.example.com's vhost to point to the /clients/root/app/ folder instead. There's a directive called DocumentRoot that you'd use to point a vhost to a where it's root folder is.
In the case of the first instance, where both the main domain and subdomain point to the same root, you don't need to use the P flag:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^clients\.example\.com$
RewriteRule ^ /clients/root/app%{REQUEST_URI} [L]
Is good enough and circumvents the need to go through mod_proxy.

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]