htaccess Redirect from folder to external URL - apache

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.

Related

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.

redirect exact url to another url

There are a bazillion examples online of doing redirects via apache's htaccess, but I can't find any example of redirecting a full URL match to another full URL.
For instance, I have an existing website at
https://example.com
How do I redirect some specific URLs for that domain to a different one:
https://example.com/login --> https://my.example.com/login
https://example.com/register --> https://my.example.com/register
For every other every other path on example.com I need to remain untouched so my site still works fine (i.e. /blog shouldn't redirect somewhere else).
You can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(login|register)/?$ http://my.example.com/$1 [L,R]
This will redirect example.com/login or example.com/register to http://my.example.com/
You can alternatively accomplish this using the RedirectMatch directive (if the newurl is on a diffrent webserver) :
RedirectMatch ^/(login|register)/?$ http://my.example.com/$1

Apache mod_rewrite links proxy

I want to make a proxy for external links with apache's mod_rewrite module.
I want it to redirect user from, ie http://stackoverflow.com/go/http://example.com/ to http://example.com/ where http://stackoverflow.com/ is my site's URL. So I added a rule to .htaccess file.
RewriteRule ^/go/http://(.+) http://$1 [R=302,L]
But it doesn't work at all. How to fix this?
I am not sure if Apache or the browser reduces // to /, but since it doesn't change the directory one of them reduces this to a single slash on my setup. That's why the second slash has a ? behind it in the rule below:
RewriteRule ^go/http://?(.*)$ http://$1 [R,L]
This will redirect the user to that domain.
This will rewrite all urls (without the beginning http://) to new complete URL. If you're gonna use https links also, you need something like the second rule.
RewriteRule ^go/(.*) http://$1 [R=302,L,QSA,NE]
RewriteRule ^gos/(.*) https://$1 [R=302,L,QSA,NE]
I also added the QSA if your need to include parameters

Apache mod_rewrite - want a file system redirect not a 301 redirect

I have example1.com on a shared web host running Apache. It has a directory example1.com/foo. I now want example2.com to serve the same content from example1.com/foo, except at the example2.com root without the intervening directory in the URL. Like example2.com/bar.html should serve the same content as example1.com/foo/bar.html .
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule ^(.*)$ foo/$1 [NC]
This simple rewrite rule takes any request intended for example2.com and inserts the foo/ to point to the content which is in that directory. Problem is this keeps doing an external 301 redirect. I don't want that, I want the browser to stay on example2.com without redirecting while Apache serves up the content from /foo in the filesystem.
Been over the Apache mod_rewrite docs several times, which say how to force a 301 redirect with the [R] flag but don't say how to force it NOT to happen. What am I missing here? It is behaving the same on both my Linux shared host and a local test with Apache on Windows.
Thanks!
I figured this out. The 301 was happening because I had the directory name wrong in the rule. So the result of the rule pointed to a path that didn't exist, which makes Apache try to fallback from the file system redirect to a 301 redirect.
Then I had to fix an infinite loop, since that above rule always adds "foo" to the URL even if it's already present so I'd get foo/foo/foo/foo/... . We need to add it only if it's not already there. Had to do it with this two-step rule, because you can't use wildcards in a capturing group of a negative rule. But this seems to work, adding "foo" when the host is example2.com and the URL does not already contain "foo".
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule !^foo - [C]
RewriteRule ^(.*)$ foo/$1 [NC,PT]

Redirect domain1 to domain2 with path after slash

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/