Special 301 redirection rules - .htaccess

I'm moving an old site to a new domain and I've been trying to make a proper 301 redirect in the .htaccess file to accomodate the kind of redirection rules below but I think I'm stumped.
new.com --> new.com/main
www.new.com --> new.com/main
old.com --> new.com/main
www.old.com --> new.com/main
old.com/* --> new.com/*
www.old.com/* --> new.com/*
sub.old.com/* --> sub.new.com/*
For the first part, it seems this code works:
# RewriteCond %{HTTP_HOST} ^new\.com$ [OR]
# RewriteCond %{HTTP_HOST} ^www\.new\.com$
# RewriteRule ^/?$ "http\:\/\/new\.com\/main" [R=301,L]
# RewriteRule ^$ http://www.new.com/main [R=301,L]
Put simple, if the browser requests any page other than the old.com domain's homepage, I'd like it to go to new.com. If someone were to visit the new.com, they'd be redirected to the /main folder. However, I'm worried that should anyone ever explicitly visit new.com/main, they'd fall into an infinite redirect.
Any help would be much appreciated.

This is code needed on DOCUMENT_ROOT/.htaccess on new.com:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?new\.com$ [NC]
RewriteRule ^$ http://new.com/main [R=301,L]
This is code needed on DOCUMENT_ROOT/.htaccess on old.com:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^$ http://new.com/main [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^(.+)$ http://new.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([^.]+)\.old\.com$ [NC]
RewriteRule ^$ http://%1.new.com/main [R=301,L]
RewriteCond %{HTTP_HOST} ^([^.]+)\.old\.com$ [NC]
RewriteRule ^(.+)$ http://%1.new.com/$1 [R=301,L]

Related

Multiple rewrite rules to different pages

I am trying to create multiple rewrite rules, so that a few pages will be redirected to certain pages, and the rest will be redirected to the start page. However, all my pages keep getting redirected to the start page.
This is the code I am using:
RewriteCond %{HTTP_HOST} ^site\.com/category\.php?s=1$ [NC]
RewriteRule (.*) http://site.co.uk/category/? [R=301,L]
RewriteCond %{HTTP_HOST} ^site\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.site\.com$ [NC]
RewriteRule (.*) http://site.co.uk/? [R=301,L]
Edit:
This is the full .htaccess:
Order deny,allow
DirectoryIndex default.php index.php
SetEnv DEFAULT_PHP_VERSION 5
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /flavours\.php?\s=1 [NC]
RewriteRule ^ http://site.co.uk/flavours/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?site\.com$ [NC]
RewriteRule ^ http://site.co.uk/? [R=301,L]
This is the link I am trying to access: www.site.com/flavours.php?s=1
HTTP_HOST cannot match REQUES_URI.
You can use:
# specific redirects
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /flavours\.php\?s=1 [NC]
RewriteRule ^ http://site.co.uk/flavours/? [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?flaverco\.com$ [NC]
RewriteRule ^ http://site.co.uk/? [R=301,L]
Make sure to clear your browser cache before testing this.

change to https and redirect to subfolder rule in .htaccess

I am trying to do two things here:
redirect to a sub-folder
redirect http://www.something.com/some/page.html
or
https://www.something.com/some/page.html
to
https://www.something.com/subfolder/some/page.html
redirect http to https
redirect
http://www.something.com/subfolder/some/page.html
to
https://www.something.com/subfolder/some/page.html
And I want to do both of them in the same .htaccess file
I have been able to redirect to subfolder by the following code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} something.com [NC]
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC]
And then I am trying to do both of them; i.e. http to https(only if http request comes) and redirect to subfolder by the following code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} something.com [NC]
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC]
But it's not working.
What am I doing wrong here?
EDIT
When using #starkeen's solution; i.e.
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ https://www.example.com/subfolder/$1 [R=301,NC,L]
I am expecting the following as result:
https://www.example.com/subfolder/brands/omega.html
when I give any of the following:
http://example.com/brands/omega.html OK
https://example.com/brands/omega.html OK
http://www.example.com/brands/omega.html OK
https://www.example.com/brands/omega.html OK
http://example.com/subfolder/brands/omega.html WRONG
http://www.example.com/subfolder/brands/omega.html WRONG
But the last two are redirecting to
https://www.example.com/subfolder/
Here is a rule to do both the tasks in a single rule:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} !^/subfolder [NC]
RewriteRule ^(?:subfolder)?(/.*)?$ https://www.example.com/subfolder$1 [NE,L,R=302,NC]
Make sure to clear your browser cache before testing this rule.
Try :
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#--Http ==>https--#
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
#--exclude the destination to avoid redirect loop--#
RewriteCond %{REQUEST_URI} !^/subfolder
#--redirect /foo to /subfolder/foo--#
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC,L]
Clear your browser'cache before testing this redirect.

301 redirection - front page to subdirectory

I needed to redirect my od domain to a new one. All paths are same on both domains except the front page, which needed to be redirected from www.mydomain.com to www.mydomain2.com/newpath. I googled and came up with this code which works. My question is if it is valid and if all pageranks will be transfered without problems. Thank you
RewriteEngine on
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.domain1.com/folder/ [L,R=301]
RewriteRule ^(.*)$ http://www.domain1.com/$1 [L,R=301]
Your code should work but it can be fine tuned a bit. Please consider this refactored code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
Rewriterule ^$ http://www.domain1.com/folder/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [L,R=301]

A simple mod re-write rule doesnt work with htaccess

I want the user to be redirected whenever he reaches my subdomain
Here is what is inside my htaccess:
RewriteEngine On
RewriteRule ^http://smale.deals.com/(.*) http://traual.deals.com/$1 [R=301,L]
RewriteRule ^http://deals.com/smale/(.*) http://deals.com/traual/$1 [R=301,L]
But no redirect happens. why?
I also have got this in my root htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (Android|iPhone|iPod|Blackberry) [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteRule ^(.*)/?$ mobile/$1 [L]
You cannot include the protocol and domain in RewriteRule. Those need to be accounted for in RewriteCond:
RewriteEngine On
# Rewrite requests to smale.deals.com to traual.deals.com
RewriteCond %{HTTP_HOST} ^smale\.deals\.com$ [NC]
RewriteRule (.*) http://traual.deals.com/$1 [R=301,L]
# For deals.com...
RewriteCond %{HTTP_HOST} ^deals\.com$ [NC]
# Rewrite requests to smale/ to deals.com/traual/
RewriteRule ^smale/(.*) http://deals.com/traual/$1 [R=301,L]

Very easy htaccess redirect works in the opposite way

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://agilerent.com$ [NC]
RewriteRule ^(.*)$ http://www.agilerent.com/$1 [R=301,L]
this is not working, while this is working:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://agilerent.com$ [NC]
RewriteRule ^(.*)$ http://www.agilerent.com/$1 [R=301,L]
with the negation in front of the condition. I've read a lot of material about .htaccess in last hour, but I can't realize yet what am i doing wrong...
%{HTTP_HOST} contains domain name ONLY, e.g. example.com, www.example.com etc and does NOT include protocol. Therefore:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^agilerent\.com$ [NC]
RewriteRule ^(.*)$ http://www.agilerent.com/$1 [R=301,L]

Resources