.htaccess rewrite url to main domain from subdomain - .htaccess

I have a domain like:
this.that.com
(it can also be accessed from:)
that.com/this
What I would like to do is make it so that the server transfers all requests from the two above url's to:
this.com
I need it so if I type in this.that.com
it will auto transfer to this.com
thanks in advance.

To redirect all requests to this.com, match everything, which is not already this.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^this\.com$
RewriteRule .* http://this.com/$0 [R,L]
If you want to redirect to the main page instead, leave out the URL path
RewriteEngine On
RewriteCond %{HTTP_HOST} !^this\.com$
RewriteRule .* http://this.com/ [R,L]
If everything works as expected, you can switch to R=301 redirection.

Related

Adding parameters to a .htaccess redirect

I'm trying to redirect a url but I can't seem to make it work for some reason. I'm trying to redirect my.domain.com/rss/abc/ to my.domain.com/rss/abc/?type=555
I've put this in my .htaccess
RewriteEngine On
# Redirects rss
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/rss/abc/?$ /rss/abc/?type=555 [R=301,L]
Any pointers ? This is on a old TYPO3 site. Anyway it could hijack the request before doing the redirect ?
I can only recommend to use some kind of online htaccess rewrite rule checker, for example https://htaccess.madewithlove.com/ (not the only one, and no real preferance or recommendation about others).
Your rewrite rule is wrong, it should be:
RewriteEngine On
# Redirects rss
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^rss/abc/?$ /rss/abc/?type=555 [R=301,L]

Redirect Specific Wildcard Subdomain to a specific url of another domain

I want to redirect specific wildcard subdomain to a specific url of another domain. the both domains are on the same server/public_html, and redirect all the inner pages of the wildcard subdomain to the inner pages of the other domain.
e.g
redirect music.example.com to stackoverflow.com/music/
and
redirect music.example.com/happy-birthday/ to stackoverflow.com/happy-birthday
redirect music.example.com/sing-to-you/ to stackexchange.com/sing-to-you/
I have more than 100k post so i cannot make the redirection of the inner pages one by one using .htaccess
Please help me out,
I tried the below code
RewriteCond %{HTTP_HOST} ^(music.example.com) [NC]
RewriteRule ^()$ https://www.stackoverflow.com/music/ [R=301,L]
but it only redirect the homepage to the specific url which I wanted, but the rest of the urls is the main issue, If I use the normal domain redirection, it works for the rest of the urls, but the function of the code above will stop working
Here is the below code for normal domain redirect redirection
RewriteCond %{HTTP_HOST} ^music\.domain.com\.com [NC]
RewriteRule ^(.*)$ https://wwww.stackoverflow.com/$1 [L,R=301,NC]
I used the below htaccess code and it works perfectly.
RewriteEngine On
# "music.example.com/" to "another.example/music/"
RewriteCond %{HTTP_HOST} ^music\.example\.com [NC]
RewriteRule ^$ https://another.example/music/ [R=302,L]
# "music.example.com/<something>" to "another.example/<something>"
RewriteCond %{HTTP_HOST} ^music\.example\.com [NC]
RewriteRule (.+) https://another.example/$1 [R=302,L]
The RewriteCond (condition) directive checks the requested host. The RewriteRule naturally redirects to the other domain. In the second rule the URL-path from the request is captured in the $1 backreference.

htaccess redirect entire domain to another, but make a custom redirection for specific url

I need to make some redirection for a domain.
Basically what I need is explained below.
Redirect entire domain.net to domain.com, but leave a custom redirect, like this:
domain.net/specific_url.html to domain.com/my_new_url.html
You can try this one.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net
RewriteRule ^(.*)$ http://example.com [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net
RewriteRule ^specific_url/? http://example.com/new_url [R=301,L]

How to .htaccess redirect domain to subdomain while keeping the main domain intact

I want to redirect all urls under my domain
domain.com/urls, www.domain.com/profile/user1.html etc. to subdomain.domain.com/urls and subdomain.domain.com/profile/user1.html etc.
but I dont want to redirect domain.com and www.domain.com to subdomain.domain.com
Is it possible via htaccess?
Edit: I want to redirect just the internal pages and files only. But leaving the main domain.com intact.
More Examples
domain.com/page1.html to subdomain.domain.com/page1.html
www.domain.com/members/admin.html to subdomain.domain.com/members/admin.html
www.domain.com to www.domain.com (no redirection here)
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/.+
RewriteCond %{REQUEST_URI} !^/index\.(php|htm)
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$ [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [L,R]
If you want to permanently redirect, change the square brackets to: [L,R=301].
You can first handle the main page by not rewriting and redirect everything else with a second rule
RewriteRule ^$ - [L]
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$
RewriteRule ^.+$ http://subdomain.domain.com/$0 [R,L]
When everything works as you expect, you can change R to R=301.

Subdomain Redirect

I want write a rule in .htaccess for redirecting
http://www.dir.domain.com
http://dir.domain.com
http://www.domain.com/dir
these should redirect to
http://domain.com/dir
Thanks in advance...
The trick here is to use an .htaccess file in your DOCROOT
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?dir\.domain\.com$
RewriteRule ^.* http://domain.com/dir/$0 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^dir/.* http://domain.com/$0 [R=301,L]
Note that this is doing a 301 redirect -- that is the server tells the browser that this is a permanent redirect and the browser caches this redirect.
You can also do an internal redirect where this mapping is done silently at the server end by tweaking flags.
This assumes that your shared service is using Apache, if its using IIS, then you need to do something similar with the web.config
You can try:
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$
RewriteRule ^ http://domain.com/%1 [L,NE]
Please change domain by your domain, when you enter sub1.domain.com then it will redirect to domain.com/sub1, when you enter sub2.domain.com then it will redirect to domain.com/sub2 ....

Resources