301 redirecting while preserving path structure - .htaccess

I am trying to redirect every URL from my old site to my new site, so that, if someone goes to say oldsite.com/about it redirects to store.newsite.com/about rather than just store.newsite.com/index.php
I tried the following code but for some reason it always takes me to store.newsite.com/index.php when I go to oldsite.com/about instead of taking me to store.newsite.com/about, and the same goes for every other path on the site. Is there something wrong with what I am putting in my .htaccess file?
Thank you!
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC]
RewriteRule ^(.*)$ http://store.newsite.com/$1 [L,R=301,NC]

Just fixed it with this code
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.site$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.old\.site$ [NC]
RewriteRule ^(.*)$ http://store.newsite.com/$1 [R=301,L]

Related

How to strip everything after first '/' In .htaccess file

I am using the below in my .htaccess file to redirect all traffic from an old domain to a new domain.
All traffic should go to the home page.
RewriteCond %{http_host} ^http://www.olddomain.co.uk/ [NC,OR]
RewriteCond %{http_host} ^www.olddomain.co.uk/ [NC,OR]
RewriteCond %{http_host} ^olddomain.co.uk/ [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk/ [R=301,NC,L]
This works fine for the homepage but for the other pages this doesn't work as the urls look something like:
http://www.olddomain.org.uk/index.php?p=214&pp=3&page=Volunteer
These are redirecting to an error page as opposed to a homepage.
I want to strip everything out that comes after the first / after the domain name but I do not know how to do this.
Thanks for your time and help in advance.
You can use:
RewriteCond %{http_host} ^(www.)?olddomain\.co\.uk [NC]
RewriteRule ^ http://www.newdomain.co.uk/? [R=301,NC,L]

.htaccess redirects - map all URLs but give homepage specific redirect

I have the following set up for an old domain, which works really well to mapping the old pages to the ones on the new domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldurl.org.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.org.uk [NC]
RewriteRule ^(.*)$ http://www.newurl.co.uk/$1 [L,R=301,NC]
The problem is I need to redirect the homepage to a specific page on the new URL, keeping the above in tact. Whats the best way to do this?
Thanks in advance!!
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org\.uk$ [NC]
RewriteRule ^$ http://www.newurl.co.uk/specificPage [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org\.uk$ [NC]
RewriteRule ^(.+)$ http://www.newurl.co.uk/$1 [L,R=301]

Rewrite www.domainnamenumberone.com/wordpress/path/ to domaintwo.com/wordpress/path

I have two domain names for the same website, and need to do a correct rewrite, so that whenever someone accesses the first domain name and all subdirectories, with, or without www. they get redirected to the second domain and subdirectories without www.
I managed to set the redirect for the domain name without subdirectories, but for whatever reason, subdirectories are not getting rewritten.
So when I go to domainnamenumberone.com, or www.domainnamenumberone.com, i get redirected to domaintwo.com – however, when I go to domainnamenumberone.com/wordpress/path or www.domainnamenumberone.com/wordpress/path I remain there, and nothing gets rewritten.
Here's what I placed in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
Would be grateful for your help!
You need to place this rule as very first rule in DocumentRoot/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainnumberone\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(\S*)\s [NC]
RewriteRule ^ http://domaintwo.com/%1 [L,R=302,NE]
Then add this line in each child .htaccess like wordpress/ or wordpress/path/ (wherever .htaccess already exists) below RewriteEngine On line
RewriteOptions Inherit
You can use that:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
But it seems to me that it should also work with yours.....
try a different browser (cache problem)

.htaccess redirect: www not working

I want to redirect a domain alias to a specific page on my site. I use this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^afdekfolie.be$ [NC]
RewriteRule ^(.*)$ http://www.technosales.be/wordpress/afdekfolie/ [R=301,L]
This works when entering afdekfolie.be as the alias, but not when entering www.afdekfolie.be
How do I make this work?
Thanks,
Stefaan
Because you are asking this rule to work for the host starting with afdekfolie...
Try this one
RewriteCond %{HTTP_HOST} ^(www\.)?afdekfolie\.be$ [NC]
Or you can break it into two rules
RewriteCond %{HTTP_HOST} ^afdekfolie\.be$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.afdekfolie\.be$ [NC]
ps: how is this related to the php tag?

RewriteRule results in infinite redirection loop

The title doesn't give justice to the question.
I have a site and I want it to be redirected to a subfolder.
I have this right now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysite.net$
RewriteRule ^(.*)$ "http\:\/\/www\.mysite\.net\/subfolder$1" [R=301,L]
The problem is that it results in a infinite redirect loop. It gets to / and redirects it to a subfolder and when it arrives there, it again redirects it.
How do I stop the redirection as soon as it gets to my designated subfolder.
I think I got it right now:
RewriteEngine on
RewriteRule !^(subfolder/) http://www.mysite.net/subfolder/ [L,R]
Got a little inspiration from a deleted answer here:
Add an exclusion rule for the target.
RewriteEngine on
RewriteRule ^/subfolder - [L]
RewriteCond %{HTTP_HOST} ^mysite.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysite.net$
RewriteRule ^(.*)$ "http\:\/\/www\.mysite\.net\/subfolder$1" [R=301,L]
Also, for the RewriteCond, you can do this:
RewriteCond %{HTTP_HOST} ^(www.)?mysite.net$
to save a line

Resources