Apache redirect sub domain to main domain conditionally - apache

I have one domain and a sub domain like cloud.example.com and example.com.
I need to redirect all cloud.example.com to example.com with the following condition.
cloud.example.com to www.example.com/customers/login
cloud.example.com/<any url> to www.example.com/<any url>
I have added the bellow code in my apache config file of cloud.example.com
ServerName cloud.example.com
Redirect 301 / https://www.example.com/customer/login
Now cloud.example.com successfully redirecting to https://www.example.com/customer/login but all other pages are not working .That are redirecting to www.example.com/customer/login
How to solve this issue?

The best way is to use rewrite rules.
RewriteRule ^/$ https://www.example.com/customer/login [R]
RewriteRule ^/(.+) https://www.example.com/$1 [R]
Dont forget to add the module and enable the RewriteEngine

Related

How to do this Apache mod-rewrite redirect transparently

I have this redirect (in Apache 2.4 VirtualHost *:80 configurations), which redirects example.com over to example.com/api/ (subfolder) and it works flawlessly.
Once I enter http://example.com into the browser, it takes me directly to http://example.com/api/.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /api$1 [R,L]
Now, what I need is for this to work transparently. I want http://example.com/api to load directly on the root domain: http://example.com
I've tried all kinds of suggestions I found on the internet, but nothing that would hide/mask the subfolder from the final URL that shows in the browser.
I figured there is an easier solution to this. I am running my app on a Tomcat server behind Apache and thus adding the following to Apache rules into VirtualHost config fixed the issue with subfolder transparency.
ProxyAddHeaders off
ProxyPass / http://server-host-for-example-com:8080/connect/
ProxyPassReverse / http://server-host-for-example-com:8080/connect/

mod_alias vs mod_rewrite precedence

I have an apache config file making use of both mod_alias's Redirect directive, and mod_rewrite's RewriteRule.
From my understanding, mod_alias works in order of top to bottom of the file, and mod_rewrite the opposite, the last rule is matched, unless you terminate with the [L] flag.
My question is, my Redirect rules are placed above the RewriteRule, yet the RewriteRule still seems to be being applied, so I end up redirecting everyone to the same url.
What I am trying to achieve is redirect certain locations to another webserver, and have a catch-all with mod-rewrite so a snippet looks like this
<VirtualHost *:80>
ServerName example.com
Redirect permanent "/foo/bar/location1/" "https://example2.com/foo/baz/location1"
Redirect permanent "/foo/bar/location2/abc" "https://example2.com/foo/baz/location2/def"
RewriteEngine on
RewriteRule ^.*$ "https://example2.com/catch-all/? [R=301, L]
Also, do these behave differently if they are inside a block or outside?
You have to write Redirect rule after Rewrite rule as it will first check what is the rewrite rule it has to follow and then the redirect rules.
<VirtualHost *:80>
ServerName example.com
RewriteEngine on
RewriteRule ^.*$ "https://example2.com/catch-all/? [R=301, L]
Redirect permanent "/foo/bar/location1/" "https://example2.com/foo/baz/location1"
Redirect permanent "/foo/bar/location2/abc" "https://example2.com/foo/baz/location2/def"

Forcing redirect 301 from any domain urls to one only subfolder

i have an old domain
www.olddomain.com
and i want to redirect to
www.newdomain.com/slug1
BUT
if a user types
www.olddomain.com/folderX
she gets
www.newdomain.com/slug1folderX
instead of simply
newdomain.com/slug1
which is wrong of course
how can i fix my apache? is it doable in the .conf files or htaccess?
UPDATE
this is my current virtual host .conf
<VirtualHost *:80>
ServerName olddomain.it
ServerAlias www.olddomain.it
Redirect 301 / https://www.newdomain.it/slug
</VirtualHost>
On old domain's site root .htaccess you can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://www.newdomain.com/slug1? [L,R=302]
Remember to clear your browser cache before testing this rule.

Redirecting base url only using htaccess

I'd like to redirect only the base url to an external site.
For instance, I want example.com redirected to anotherdomain.com but I don't want example.com/path to be redirected.
So far, example.com/path redirects to anotherdomain.com/path. :(
EDIT :
First, thank you for the help! example.com now redirects to another.com without affecting the children paths of example.com.
However, ideally, m.example.com won't redirect to another.com. So it's really just example.com redirecting to another.com.
Add this to your .htaccess in your DocumentRoot. I am assuming that you are hosting only one domain on the server.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^$ http://anotherdomain.com [R,L]

Need help configuring 301 permanent redirect in Apache for non www

I am trying to configure my Apache 2.2 version to use a 301 permanent redirect when someone types my url without the www. I want to configure this in the httpd.conf and not using .htaccess if possible. I have tried using Redirect permanent but the first variable has to be a directory and not a url. Any ideas how to configure boom.com requests to be redirected to www.boom.com using a 301 redirect in Apache? Thanks
Add the following:
# Canonical hostnames
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.boom\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^/(.*) http://www.boom.com/$1 [L,R=301]
This will redirect all request which don't match www.boom.com to www.boom.com, with the same query path. (For example, boom.com/foo?foo=bar will be redirect to www.boom.com/foo?foo=bar).
If you have named virtual hosts you could put the extra RewriteCond entries #tux21b gave inside to isolate them. Also if you have mod_alias you could try this which should do the same thing:
<VirtualHost boom.com:80>
RedirectMatch permanent /.* http://www.boom.com$0
</VirtualHost>
I'm sure someone will comment if there's a reason to use one over the other.