Ok I am serving two domains off of one box. The domains are:
www.old.org and www.new.com. As it is now, all of the files and dirs are the same on both domains.
I want to make it so that IF someone goes to www.old.org/folder_a/file_b.php they are 301'ed to www.new.com/folder_a/file_b.php.
I've already tried in the htaccess:
RedirectMatch 301 ^/ http://www.new.com/
But that give a 301 loop because the 301's condition still applies after the 301 is enacted. I think I want to do something that uses rewritecond %{HTTP_HOST} ^.*old.org$ so that only url's at old.org or www.old.org will be affected, but I'm not sure how to do this.
If you have access to the apache vhost configs use those instead of the .htaccess:
<VirtualHost *:80>
ServerName www.old.org
Redirect permanent / http://www.new.com/
</VirtualHost>
If you really must use an .htaccess the following will do:
RewriteEngine On
RewriteCond %{SERVER_NAME} =www.old.org [NC]
RewriteRule (.*) http://www.new.com/$1 [R=301,L]
Some thing like this should do:
RewriteCond %{http_host} ^www\.old\.org$ [NC]
RewriteRule ^/(.*) http://www.new.com/$1 [R=301]
Related
I need to redirect subfolder to subdomain using htaccess
For example:
redirect www.example.com/test (also http://, https://, http://www, https://www)
to
https://user.example.com
how do I do it?
also, how it should be if I need to redirect it to https://user.example.com/test
You can put this code in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^test/?$ https://user.example.com/test [R=301,L]
Don't forget to enable mod_rewrite if it's not yet done
Look at original answer at
Can you do a different for blog.example.com?
If you can, just put your Redirect config there:
<VirtualHost *:80>
ServerName blog.example.com
Redirect / http://example.com/blog/
</VirtualHost>
If for some reason you still need to piggy-back on the other vhost, or need to handle all subdomains and not just "blog", then use mod_rewrite:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$
RewriteRule ^/(.*)$ http://example.com/%1/$1 [R=301,L]
I am trying to redirect the following (respectively):
http://sub.firstdomain.com/d/(all_files_and_folders)
http://sub.firstdomain.com/d2/(all_files_and_folders)
to
http://sub.seconddomain.com/d/(all_files_and_folders)
http://sub.seconddomain.com/d2/(all_files_and_folders)
There are other files and folders under the first subdomain that I do not want redirected. Previously this was working but now it seems like Go Daddy changed something and what I had is no longer working. Here it is:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^stats/(.+) /dstats/count.php?statspage=$1 [L,NC,QSA]
RewriteRule ^(.+)\.deb$ /dstats/count.php?file=$1.deb [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(d2?)/(.*)$ http://sub.seconddomain.com/$1/$2 [L,R=301]
You can ignore the RewriteRule. It is working fine. I wanted to make sure I included the entire .htaccess file just in case. My issue is with RewriteCond it seems.
The following should work:
RewriteEngine On
Redirect 301 /d/ http://sub.seconddomain.com/d/
Redirect 301 /d2/ http://sub.seconddomain.com/d2/
The above should only redirect anything in the /d/ and /d2/ folder of the sub subdomain.
EDIT: Second try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub1\.
RewriteRule ^(d2?)/(.*)$ http://sub2.mydomain.com/$1/$2 [L,R=301]
I have a site example.com. Any traffic to www.example.com is redirected to example.com in the .htaccess file using:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
This piece of code that I swiped of the interweb works fine.
I have now added a subdomain subdom.example.com. Similar, any traffic to www.subdom.example.com should be redirected to the non www canonical version.
The following code does not work:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://subdom.example.com/$1 [R=301,L]
Presumably redirects work a little differently where subdomains are involved. Could anyone share how I would edit the above snippet to redirect any www. traffic to the canonical non www. subdomain version?
I believe you don't have sufficient RewriteCond conditions for both redirects. What is happening is that first one is unconditional redirect and since it appears first it always fires and 2nd one never fires.
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Above code will work with both of your domains.
When I enter my website address, it does not always put www. in front of it.
This is because I type domain.com instead of adding the www. in front as this is quicker.
What code do I have to use to make sure that even if I type domain.com it always adds the www. infront.
(I believe this has to do with .htaccess mod_rewrite function?
Thank you,
Chad.
In your .htaccess file, add the following lines (replacing mywebsite.com with your real website):
<IfModule mod_rewrite.c>
RewriteEngine On
#redirects all requests to www.mywebsite.com
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com
RewriteRule (.*) http://www.mywebsite.com/$1 [R=301,L]
</IfModule>
Note that as google recommands it is better to use a 301 redirection.
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com[nc]
RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
I have a list of 301 Redirects like this inside htaccess:
Redirect 301 /oldpage1.php http://www.domain.com/newpage1.php
Redirect 301 /oldpage2.php http://www.domain.com/newpage2.php
Redirect 301 /oldpage3.php http://www.domain.com/newpage3.php
...
Now these pages shall be redirected only for a certain domain (there are other domains now pointing to the same location like www.domain.com).
E.g. www.domain.com/oldpage1.php shall be redirected, but www.sub.domain.com/oldpage1.php not.
So whats the way to apply these redirects only for www.domain.com?
Thanks.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage1\.php/?$ newpage1.php [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage2\.php/?$ newpage2.php [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage3\.php/?$ newpage3.php [L,R=301]
PS: If you have several rules like above then it is much better to use RewriteMap.