301 Redirect from Magento to new store - apache

I have a magento store set up at www.mysite.com/store/ - this is a Magento store and I want to redirect everything to a new domain www.mysiteshop.com.
I've put a .htaccess in the root of the Magento directory so that anyone who comes to it get redirected to the new store domain.
I don't need to redirect visitors who come to www.mysite.com.
RewriteRule (.*) http://www.mysiteshop.com/$1 [R=301,L]
But I need to make some changes as to how it works.
First, if they visit www.mysite.com/shop/alarm-clock.html I need to redirect them to www.mysiteshop.com/products/alarm-clock.
I also want to still be able to access the old admin area so if I could not redirect the url www.mysite.com/shop/index.php/admin/

If your htaccess file with the mysiteshop.com rule is in the /store/ directory, then you'll need to create another htaccess file in the document root (this is where you end up if you go to http://www.mysite.com/) in order to handle redirects for /shop/:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/shop/index\.php/admin
RewriteRule ^shop/(.+)\.html$ http://www.mysiteshop.com/products/$1 [L,R=301]
Or, you can place the rules in the htaccess file in the /shop/ directory:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/shop/index\.php/admin
RewriteRule ^(.+)\.html$ http://www.mysiteshop.com/products/$1 [L,R=301]

Related

redirectMatch in mod rewrite

We recently moved our web site to another server, say http://newdomain.com, while our old site is at say http://olddomain.com
I would like to keep the users directory on the old site accessible as it was before, that is http://oldomain.com/~user, but anything accessible under the root folder point to the new domain. The users directory on the old server are in the /home/user file system. So, I wrote following rules:
RedirectMatch permanent ^/~(.*) /home/~$1
RedirectMatch permanent ^/(.*) http://newdomain.com/$1
while the second rule works flawlessly, the first one still wants to map to the new site as following: http://newdomain.com/~user.
How can I fix the two rules so that anything in /var/www/html on the old site redirects to the new site, but anything under /home does not redirect?
--
Here is the new code based on Ben's solution, which maps the urls for the access to users' home pages correctly, but the browser complains and does not show their sites. The root folder urls redirection works fine.
RewriteEngine on
# RewriteBase / -- I had to comment this as the apache did not like it
Rewriterule ^/~(.*) /~$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/home/~
RewriteRule ^/(.*) http://newdomain.com/$1 [R=301,L]
You should use RewriteCond to test if the URI prefix with /home/~, and use RewriteRule to rewrite the URL and make a permanent redirect.
RewriteEngine On
RewriteBase /
RewriteRule ^~(.*) /home/~$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/home/~
RewriteRule ^(.*) http://newdomain.com/$1 [R=301,L]
Line 3: if URI prefix with ~{any_characters}, rewrite the URI to /home/~{any_characters}, redirect permanently
Line 5: Test if the URI not prefix with /home/~{any_characters}
Line 6: Then process the rewrite rule, rewrite the URI from {any_characters} to http://newdomain.com/{any_characters}, redirect permanently

having trouble writing htaccess redirects

I'm having trouble writing redirect rules in my website's htaccess file.
Basically, i want to write two rules:
1 - When i write the base URL, like http://www.example.com, i want it to automatically redirect the user to http://www.example.com/someDirectory.
2 - However, when i write http://www.example.com/Admin, i want it to redirect the user to http://www.example.com/Admin.
Here's what i've managed to do so far:
# This allows you to redirect index.html to a specific subfolder
Redirect http://www.example.pt http://www.example.pt/MainFolder
# This allows you to redirect index.html to a specific subfolder
Redirect http://www.example.pt/Admin http://www.example.pt/Admin
However this does not work... Any idea on how to do this?
Try it like this,
When there is no request for specific file or directory it will redirect you to your directory mention in rule and for the rest it will work without any rule.
Please check.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^ %{HTTP_HOST}/someDirectory [R,L]
After a long research i was able to find a solution to my problem. I'll leave it here, in case anyone's having the same problem:
#Rewrite everything to subfolder
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/MainFolder
RewriteCond %{REQUEST_URI} !^/Admin
Rewriterule ^(.*)$ MainFolder/$1 [L]

Redirect all pages in direcory except one subdirectory .htaccess

I want to have one URL still being served from a subdirectory on the old domain but redirect all others from the parent directory to the new domain.
www.olddomain.co.uk/directorya/directoryb -> no redirect
www.olddomain.co.uk/directorya -> www.newdomain.co.uk/directorya
This works if the URL is typed with a trailing slash
RewriteCond %{REQUEST_URI} !^/directorya\/directoryb [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk/ [L,R=301]
So http://www.olddomain.co.uk/directorya/directoryb/ works but http://www.olddomain.co.uk/directorya/directoryb fails.
Tried added $ at the end and other connotations and tried
RewriteEngine on
RewriteRule !^myspecialdirectory/mynextdirectory($|/) http://newdomain.example%{REQUEST_URI} [L,R=301]
from Redirect entire site except one directory to a new site - apache .htaccess
What do I need to do to get http://www.olddomain.co.uk/directorya/directoryb/ or http://www.olddomain.co.uk/directorya/directoryb to redirect? (while all other http://www.olddomain.co.uk pages go to http://www.newdomain.co.uk
For reasons I don't understand http://www.olddomain.co.uk/directorya/directoryb goes to Google and doesn't redirect
Try using THE_REQUEST variable and make sure this this very first rule:
RewriteCond %{THE_REQUEST} !\s+/+directorya/directoryb [NC]
RewriteRule ^ http://www.newdomain.co.uk/? [L,R=301]
Test it after clearing your browser cache.

Apache, redirect directory but keep sub-directory un-redirected

In Apache, is it possible that I can redirect a directory on my website to a new address, but keep sub-directories un-redirected?
For example, I wish to redirect accesses to files under
https://www.example.com/folder/ to
https://www.newexample.com/folder/
i.e. from
https://www.example.com/folder/abc.html to
https://www.newexample.com/folder/abc.html
But keep the access to its sub-folders unchanged?
https://www.example.com/folder/subfolder/123.html will not be redirected.
Many thanks!
You can exclude subfolder requests with a RewriteCond
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder/.*?/
RewriteRule ^folder/ https://www.newexample.com%{REQUEST_URI} [R,L]

htaccess force redirect based on cookie not working for sub-directories

I have a folder for one of my domains /site. Here I have a .htaccess file with:
rewriteengine on
rewritebase /
rewritecond %{HTTP_COOKIE} !allow=asdx
rewriterule ^.*$ .set-cookie.php
Basicly I want to redirect all requests that do not have the allow cookie set to "asdx" to another file.
The problem is that I have folders like /site/subdomain1 that have .htaccess files of their own (with rewriteengine on). Accessing one of these files renders the initial redirect (based on the cookie) useless. If I disable the rewriteengine on directive from the sub-folders the cookie-based redirect works again.
How can I make the cookie-based redirect work without actually going to the individual subfolders and adding it there?
use the following for the rewrite rule:
rewriterule ^.*$ .set-cookie.php [R=302,L]