htaccess redirect subfolder of certain domain - .htaccess

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.

Related

Redirect domain to a specific page on another domain

I have a website www.mainwebsite.com with a page located at www.mainpage.com/pagename
I need to redirect a new domian name to this page. so www.newdomainname.com should redirect to www.mainpage.com/pagename
How can I do this with the htaccess?
My site is using Perch Runway too not sure if this will change anything?
Thanks
You can achieve that by using the following rules in your .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^newdomainname\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.newdomainname\.com [NC]
RewriteRule ^(.*)$ http://www.mainpage.com/pagename/$1 [L,R=301,NC]
So if either of newdomainname.com or www.newdomainname.com it will then using a 301 redirect take you to www.mainpage.com/pagename.
Make sure you clear your cache before you test this.

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]

Automatically redirect subdomain to folder path?

Is it possible to automatically redirect subdomains to a folder (structure)? I want to change the setup of my site from subdomain to folder and there a lot of redirects to be done, so i was wondering if there is any automatic solution.
What i want is i.e.:
subfolder1.domain.com
to be redirected to (internal redirect, same top-level domain) www.domain.com/folder/subfolder1
Where 'subfolder1' is relative (not static).
So is there a general htaccess code i can use?
Thanks
You can put this code in your htaccess (which has to be in document root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.(domain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%2/folder/%1/$1 [R=301,L]
Note: by redirect in your question, i understood it as an external redirect. If that's not the case, i'll update my answer
EDIT (internal rewrite)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.domain\.com$ [NC]
RewriteRule ^((?!folder/).*)$ /folder/%1/$1 [L]

Redirect only the start page to extern site all sub categories should stay

I have a problem were I need to redirect www.mydomain.com to an external url www.otherdomain.com but I need all the subpages and urls to stay as they are (not get redirected).
Example www.mydomain.com/testpage should not get redirected. Is this possible?
And if so can anyone guide me what I need to write in my htaccess file for it to work.
The following .htaccess will do the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} www.\mydomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.otherdomain.com/ [L,R=302]
It will redirect only the base domain (root), all subfolders and other URLs will stay.
Reference:
https://serverfault.com/questions/58762/how-to-redirect-root-and-only-root-via-htaccess

Problem setting 301 redirection statements in htaccess

I'm having some trouble setting up a htaccess file. Currently I have 15 domains serving their own website. Now moving this to a single website and domain, I want to serve a htaccess with 301 rules for the old urls. The destination url depends on the domainname of the old url..
E.g.
http://www.previouswebsiteinspanish.com/contacto should be permanently rewritten to http://www.newcentralwebsite.eu/es/contact
I understand that I have to use the RewriteCond but I'm not familiar enough with it to get it working.
RewriteCond %{HTTP_REFERER} !^http://(www\.)?previouswebsiteinspanish/.*$ [NC]
RewriteRule ^([^/.]+)/contacto$ /$1/contact [R=301,L]
Thanks in advance!
You current condition is that the rule will be executed under the condition that the referring page is not on the old domain. That's most likely not what you want to do. :)
You want to check the %{HTTP_HOST} parameter, which contains only the hostname part of the currently requested URL.
Assuming you want to redirect everything on the old domain to the es subfolder, this rule will do it.
RewriteCond %{HTTP_HOST} ^(www\.)?previouswebsiteinspanish.com$ [NC]
RewriteRule ^(.*)$ http://www.newcentralwebsite.eu/es/$1 [R=301,L]
Then you'll have to add similar rules for each old domain.
RewriteCond %{HTTP_HOST} ^(www\.)?previouswebsiteinspanish\.com$ [NC]
RewriteRule ^contacto$ http://www.newcentralwebsite.eu/es/contact [R=301,L]
You need to check HTTP_HOST which is the hostname of the requested site.
From your example the rule I wrote is
RewriteCond %{HTTP_HOST} ^(www\.)?previouswebsiteinspanish\.com$ [NC]
RewriteRule ^contacto$ http://www.newcentralwebsite.eu/es/contact [R=301,L]
Add such rules to your .htaccess.
You have to write such rules for each domain.
In above case you have change in url too, so you may have to handle such special cases too.

Resources