Forwarding a subdomain to another subdomain [duplicate] - .htaccess

Does anyone know a way to do a permanent redirect from a.example.com to b.example.com? I have other subdomains that need to remain as they are though. so:
first.example.com -> second.example.com
third.example.com is fine
fourth.example.com is fine
I want to do this in the <VirtualHost> block rather than an .htaccess to avoid having to redeploy again.
Any thoughts would be appreciated, thanks

Add the following RewriteRule to your VirtualHost
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.example.com$
RewriteRule ^ http://second.example.com [R=301,L]
If you wanted to redirect first.example.com/some/url to second.example.com/some/url:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.example.com$
RewriteRule /(.*) http://second.example.com/$1 [R=301,L]
Recommend you use [R=302] whilst testing the rules to avoid problems with 301s getting cached by your browser.

Related

.htaccess redirect subdomain to php file without URL change in browser

help me please.
I have several php files in root
domain.com/dog.php
domain.com/cat.php
...
When user go to dog.domain.com i need to show domain.com/dog.php but keep dog.domain.com in browser URL
I already have this .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*).domain.com$
RewriteRule .* http://domain.com/%1.php [L]
It works fine and show domain.com/dog.php content when I type dog.domain.com, but now it changes URL in browser to domain.com/dog.php
How to solve it?
P.S. Wildcard DNS record turned ON (already catch all subdomains *.domain.com)
If *.domain.com is defined within the same vhost you have to use a relative RewriteRule there: RewriteRule ^.*$ /%1.php [L]). If not, you have to replace [L] by [P] and make sure that you have mod_proxy loaded. See https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Last but not least, you might want to update your RewriteCond to be case insensitive: RewriteCond %{HTTP_HOST} ^(.*).domain.com$ [NC]

Magento non www to www

I am pulling my hair out here.
I have created a landing page which is http://www.pps-supplements.com/samples
it works and you can access it fine - no problems.
If you type in the address bar:
pps-supplements.com/samples
it takes you to the home page which is not good and causing me to have a headache.
My website is working well for non www to www on home page and categories but not on cms pages.
I have read a few posts on here and tried their solutions which is to edit the htaccess file but it hasn't fixed it.
Does anyone have any ideas how I can resolve this issue??
Pretty please!
You could try this in your .htaccess file....
Be sure to add it above any other rewrite rules or conditions you may already have in your htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
Rewritecond %{HTTP_HOST} !^www\.pps-supplements\.com [NC]
Rewriterule (.*) http://www.pps-supplements.com/$1 [R=301]
</IfModule>
This will redirect any domain that is not www.pps-supplements.com to the version with www's
Also very handy for using when pointing multiple domains at a site.
Also, be sure that the webserver is set-up to receive the non www version, as in that it is listening for that version, as well as for the version with www's.
For anyone else looking at the accepted answer and running into issues with it not working you may have other Rewrites in place that need to be stopped from executing.
You can stop further execution by including the "L" param.
Example:
Rewritecond %{HTTP_HOST} !^www\.domain\.com [NC]
Rewriterule (.*) http://www.domain.com/$1 [R=301,L]
I wanted to redirect from example.com/category/ to www.example.com/category/
and I found solutions:
turn off Auto-redirect to Base URL in backend
and use this code in .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
</IfModule>
For Magento Multistore-Setups:
Rewritecond %{HTTP_HOST} ^example\.com [NC]
Rewriterule (.*) http://www.example.com/$1 [R=301,L]

how to redirect www.sub.domain.com to sub.domain.com?

how to redirect www.sub.domain.com to sub.domain.com?
other info:
right now sub.domain.com works, www.sub.domain.com is 'server not
found'.
im using drupal 6. I dont want to add an A record because the caching module i use (Boost) will cache a version of the site with www and without www, effectively doubling the cache size. <-- Not really sure about that but that's the case with my site at its previous host. In any case this is probably just a simple htaccess issue.
Doesn't this helps, inside .htaccess file?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
Also: https://drupal.stackexchange.com/questions/50552/how-to-keep-domain-consistency-with-or-without-www/50553#50553

In .htaccess, redirect all domains except one

I have multiple domains on my server. I want to redirect all of them to one (example.net).
My .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]
I’m redirecting all URLs on my server to one main domain, but that domain is also redirecting to itself. So www.example.net returns 301 Moved Permanently and redirects back to itself. I’m told that this isn’t good for SEO. How could I fix this?
You need to add a Rewritecond to prevent it from redirecting when you’re already on the domain that you want. There are loads of examples online if you google it, or see the RewriteCond section of Apache’s mod_rewrite documentation.
What you’re looking for is something like:
RewriteEngine on
Rewritecond %{HTTP_HOST} !^www\.example\.net
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]
Just small note:
Thanks goes to TRiG, but I had to remove one slash to make it work correctly (coz it added two slashes after domain name). This works for me:
RewriteEngine on
Rewritecond %{HTTP_HOST} !^www\.example\.net
RewriteRule ^(.*)$ http://www.example.net$1 [R=301,L]

.htaccess rewrite add sub-domain exception (remove redirect)

I have checked previous questions, I believe this is quite simple but I can't seem to work it out.
In my .htaccess file I currently have
RewriteCond %{HTTP_HOST} !^www\.bodycleansediet\.com [NC]
RewriteRule ^(.*)$ http://www.bodycleansediet.com/$1 [R=301,L]
However this is causing a problem as it also redirecting any sub-domains (specifically au.bodycleansediet.com and ca.bodycleansediet.com) to www.bodycleansediet.com
I want them NOT be to be redirected so they can be viewed on their sub-domains.
I know I need to add an exception/re-write rule but I am not sure how to construct it.
Any advice on how to construct this?
Should not something along the following lines work
RewriteCond %{HTTP_HOST} !^(www|ca|au)\.bodycleansediet\.com [NC]
RewriteRule ^(.*)$ http://www.bodycleansediet.com/$1 [R=301,L]
Since you are basically checking if not (www OR ca OR au) then do your redirect.

Resources