I currently have a website on one domain and it has another domain mapped to it. I have a .htaccess RewriteCond like the following:
RewriteCond %{HTTP_HOST} ^www\.additional-domain\.com [NC]
RewriteRule (.*) http://www.main-domain.com$1 [R=301,L]
This obviously means that any pages accessed by going to www.additional-domain.com will redirect to the correct page on www.main-domain.com.
What I would like to know is; can I keep this rule but have certain pages not redirect? so for page /this-page.html if the user accessed www.additional-domain.com/this-page.html then they are not redirected to www.main-domain.co.uk/this-page.html
Is this even possible? Thanks.
-------- EDIT --------
I neglected to mention that for those additional pages I want the redirect to work in reverse.
So if someone accessed www.main-domain.co.uk/this-page.html then they would be redirected to www.additional-domain.co.uk/this-page.html
Add extra conditions to your redirect rule:
RewriteCond %{REQUEST_URI} !/this-page\.html
RewriteCond %{REQUEST_URI} !/this-page2\.html
RewriteCond %{HTTP_HOST} ^www\.additional-domain\.com [NC]
RewriteRule (.*) http://www.main-domain.com$1 [R=301,L]
Related
I am in need of help with build an htaccess file. Basically I am moving an website from www.abc.com to www.xyz.com. Now, here is the two things that I am trying to achieve:
All users should be redirected to www.xyz.com when accessing www.abc.com
If user accesses www.abc.com/files/abcd.file or www.abc.com/files/folder/abcd.file, etc. should be redirected to arhive.xyz.com/abcd.file, etc. Basicly, this URL www.abc.com/files/, should be replaced with arhive.xyz.com, keeping the same file/directory structure.
Someone can help? Thanks in advance.
You have several requirements, but if I understand correctly this should get what you need in the htaccess file in the root directory.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteCond %{REQUEST_URI} !^/files(/.*)?$ [NC]
RewriteRule ^ http://www.newsite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteCond %{REQUEST_URI} ^/files(/.*)?$ [NC]
RewriteRule ^ http://archive.newsite.com%{REQUEST_URI} [R=301,L]
Of course change to your domain names.
I'm trying to redirect two kind of urls to a subdomain and all the others to my main domain.
A concrete example :
"my account" pages starting by /my-account/* and subscription page must be redirected to https://my-account.domaim.com. The uri must be kept.
all others like /news or the homepage must be seen on www.domain.com only
Here is what I have tried until now :
# All urls except my-account/* and subscription are redirected to the main domain
RewriteCond %{HTTP_HOST} ^my-account\.domain\.(.+)$
RewriteCond %{REQUEST_URI} !^my-account/
RewriteCond %{REQUEST_URI} !^subscription$
RewriteRule ^(.*)$ http://www.domain.%1/$1 [L,QSA,R=301]
# subscription page is redirected to my-account subdomain
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^subscription$ https://my-account.domain.%1/subscription[L,QSA,R=301]
# All my-account/* pages are redirected to my-account subdomain
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^my-account/(.*)$ https://my-account.domain.%1/my-account/$1 [L,QSA,R=301]
Each rules work independently but if i try all of them together i'm stuck in an infinite loop.
Is anyone have an idea how to prevent it ?
The rules look fine, but you have a missing space in your 2nd rule:
RewriteRule ^subscription$ https://my-account.domain.%1/subscription[L,QSA,R=301]
# -----------------------------------------------------------------^
But you can probably combine them into a single rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^(subscription|my-account/.*)$ https://my-account.domain.%1/$1 [L,QSA,R=301]
Also make sure you're clearing your cache when you test, as 301 redirects are permanent and the browser will cache them.
Thanks for your answer !
The typos was from my copy/paste and the combination works but doesn't change anything to the problem with the other rules. I keep it for later ;)
I tried a reversed rules like this :
#RewriteCond %{HTTP_HOST} !^my-account\.domain\.(.+)$ [NC]
#RewriteCond %{REQUEST_URI} ^/subscription$ [OR]
#RewriteCond %{REQUEST_URI} ^/my-account/ [NC]
#RewriteRule ^(.*)$ https://my-account.domain.%1/$1 [L,R=301]
It also works but not in combination with the other. it doesn't loop anymore but if i try something like http/www.domain.com/subscription i'm redirected to www.domain.com with the url truncated. It seems that the Rewrite conditions aren't correctly recognized but still can't find why ...
So, this is my .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.mydomain.com/folder/$1 [R=301,L]
The idea was to have www forced upon each user, no matter if the user visits my domain with or without www and at the same time, that user needs to be redirected to "folder" or in other words to www.mydomain.com/folder .
Although this code works perfectly, I want it also to redirect users to www.mydomain.com/folder when they go anywhere on mydomain.com domain except /folder. For example: (www.)mydomain.com/index.html (or any other page) and (www.)mydomain.com/anyotherfolder needs to be redirected to www.mydomain.com/folder
Thank you.
Add this additional rule after your existing rule:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule !^folder http://www.mydomain.com/folder/ [R=301,L,NC]
I'm in the process of setting up a static cookieless domain, and everything is working great!
But im wanting to know how to redirect if the request isn't to an image/js/css file, to avoid duplicate content from the cookieless domain.
I have this:
RewriteCond %{HTTP_REFERER} ^http://files\.my-static-site\.com/ [NC]
RewriteRule \.(html|php)$ - [F,NC,L]
and it works, but it still loads the home page if you simply put files.my-static-site.com.
If you click on a link on the page your shown a 403. How do i change that to 301 redirect to the main domain instead of showing a 403?
Try changing it to this:
RewriteCond %{HTTP_HOST} ^files\.my-static-site\.com$ [NC]
RewriteRule \.(html|php)$ http://main.domain.com/ [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^files\.my-static-site\.com$ [NC]
RewriteRule ^$ http://main.domain.com/ [R=301,NC,L]
I have a site that has been up for some time. I had a blog on a subdomain for some time. I have decided to do away with the main site and just support the blog subdomain.
I have a redirect setup for this, but it carries all the extra parameters through to the blog which results in a file not found page appearing. I just want the redirect to go to the index page without parameters.
What I currently have in my .htaccess file is this
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?(.*)$ "http\:\/\/blog\.foo\.org\/index\.php" [R=301,L]
When I get a request to
http://www.foo.org/foo/foo/?module=foo
it redirects to
http://blog.foo.org/foo/foo/index.php?module=foo
I want it to redirect to
http://blog.foo.org/index.php
You have to specify the query in the replacement to override the original:
RewriteCond %{HTTP_HOST} !^blog\.example\.org$ [NC]
RewriteRule ^ http://blog.example.org/index.php? [R=301,L]
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.org$ [NC]
RewriteRule ^ http://blog.foo.org/ [R=301,L]