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]
Related
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.
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]
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]
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]
I've been searching and searching with no success for this simple htaccess redirect from a non-www URL to a www.
I have the following in the site's root htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
From what I've read this should work but instead of htp://domain.com/index.html going to htp://www.domain.com/index.html, it goes to htp://www.domain.comindex.html/ - Notice the ignored forward slash between .com and index.html
I have also tried the following rewrite conditions: (not all at once obviously)
RewriteCond %{HTTP_HOST} domain.com [NC]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
Any help would be greatly appreciated.
NOTE: Domains would usually have 'http' but because of the stack overflow limit of urls, I've had to remove a 't' :)
Tested solution that just works (at least on all my production servers):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]