htaccess for subdomain no longer in use - .htaccess

I had a subdomain https://sub.rootdir.com/ which was migrated to the root domain and to a new host.
I want to redirect this subdomain to the root folder. The host told me to re-create the subdomain and then create an '.htaccess' file in which you can set redirection rules such as:
Redirect 301 / https://rootdir.com/
I have kind of no idea what to type in the htaccess file.
Can I please get help from you?

Create a .htaccess file in your subdomain folder and enter this code in;
RewriteEngine On
RewriteCond %{HTTP_HOST} !^rootdir\.com
RewriteRule (.*) https://rootdir.com/$1 [R=301,L]

I figured it out from other posts here tx
#mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.rootdir.com$ [NC]
RewriteRule ^(.*)$ https://www.rootdir.com%{REQUEST_URI} [R=301,NC,L,QSA]

Related

htaccess 301 redirect domain but not a specific folder and files

I tried searching in first here in stackoverflow and google for the best answer but I could not find a solution to my problem.
My question is can I add a condition where a specific sets of folder/files in my server will not be affected by a htaccess 301 redirection from old domain to new domain?
this is the htaccess I added.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !mysite.com.au$ [NC]
RewriteRule ^(.*)$ http://mysite.com.au/$1 [L,R=301]
the old domain was mysite.com without the .au and the above code will direct all links in my old site to be transfer to .com.au , but I want some of my folder in .com to be not affected by the htaccess 301 redirection, for example.
mysite.com/folder1/,
mysite.com/images/,
mysite.com/js/
and some files will not be affected by this redirection. Can any one point me to on how to fix or add a rule so a specific folder will not be affected by the old domain to new domain htaccess 301 redirection I added?
thanks,
You can add a RewriteCond in your redirect rule for exceptions:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(folder/|images/|js/) [NC]
RewriteCond %{HTTP_HOST} !^mysite\.com\.au$ [NC]
RewriteRule ^ http://mysite.com.au%{REQUEST_URI} [NE,L,R=301]

301 redirect subdomain to another domain with htaccess

I want to 301 redirect a subdomain to another domain with htaccess.
I want:
A: www.subdomain.domain1.se
B: subdomain.domain1.se
C: subdomain.domain1.se/anything/anything.anything#anything?anything
to redirect to:
A: www.domain2.se
B: www.domain2.se
C: www..domain2.se/anything/anything.anything#anything?anything
Also I need to know where to put the file (in subdomain directory or root directory). Best would be if I could put the htaccess file in the subdomain ddirectory if possible.
I have tried this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^c\.domain1\.se$ [NC]
RewriteRule ^(.*)$ http://www.domain2.se/$1 [QSA,R=301,L]
I think you messing RewriteBase / in the code
regular redirect 301 work like this
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} subdomain\.domain1\.se$ [NC]
RewriteRule ^ http://www.domain2.se%{REQUEST_URI} [R=301,L,NE]
Reference: Apache mod_rewrite Introduction
However note that URL part after hash is not sent to web server hence cannot be handled by Apache mod_rewrite.

htaccess redirect subfolder of certain domain

I have a multi-subdomain multi-lingual website setup and I need to redirect a language subfolder of a certain domain. eg. au.domain.com/us/request_uri to au.domain.com/en/request_uri. This rule needs to be ignored on other domains. All domains are run off the same codebase and all use the one htaccess file (drupal install with domain access module). This shouldn't affect how the htaccess rule is set though.
Here is one way to do it:
RewriteEngine On
RewriteRule ^us/(.*)$ http://au.domain.com/en/$1 [L,R=301]
Redirects:
http://au.domain.com/us/anything to
http://au.domain.com/en/anything/
UPDATED
RewriteEngine On
RewriteRule http://au.domain.com/us/^(.*)$ http://au.domain.com/en/$1 [L,R=301]
Only http://au.domain.com/us/anything will be redirected to
http://au.domain.com/en/anything/
OR
RewriteEngine On
RewriteCond %{HTTP_HOST} ^au.domain\.com$ [NC]
RewriteRule ^us/(.*)$ http://au.domain.com/en/$1 [L,R=301]
Hope this helps.

How to load the same website after redirecting subdomain to subdirectory htaccess

I could redirect subdomain to subdirectory:
sub.domain.com > www.domain.com/sub
By using:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.domain.com [NC]
RewriteRule (.*) www.domain.com/sub/$1 [R=301,L]
However, the page at www.domain.com/sub displayed nothing but this:
Index of /sub
Parent Directory
Apache Server at www.domain.com Port 80
I have a working website with contents at sub.domain.com.
How can I load the same website and make it work at www.domain.com/sub ?
Thank you.
Jon is right, you need to move the content. Then you just need to fix your redirect by adding http:// in front of the target domain to redirect incoming requests from the old subdomain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/sub/$1 [R=301,L]

Why isn't my .htaccess 301 redirect working?

I'm trying to redirect this URL to a different subdomain but am getting a 404.
Need to redirect:
www.dustystrings.com/instrumentbuilding / XYZ
To here:
manufacturing.dustystrings.com/instrumentbuilding / XYZ
I have www.dustystrings.com on one server, and manufacturing.dustystrings.com on another server (necessity).
Basically, I want to redirect all www.dustystrings.com/instrumentbuilding/ queries to manufacturing.dustystrings.com/instrumentbuilding/
What's the right .htacess 301 code to do this? (Apache server)
This should work:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.dustystrings.com[nc]
RewriteRule ^.*/instrumentbuilding/(.*)$ http://manufacturing.dustystrings.com/instrumentbuilding/$1 [r=301,nc]
Redirect to www using htaccess redirect
Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Please REPLACE domain.com and www.newdomain.com with your actual domain name.
Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Resources