htaccess redirect website - php

I'm trying to redirect all my visitors from the old domain that I use to the new one.
Here is the content of the .htaccess file that I use
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !foobar.com$ [NC]
RewriteRule ^(.*)$ http://foobar.com/$1 [L,R=301]
where http://foobar.com is the new domain.
The code above works but only if the visitor type http://olddomain.com.
What I mean is when the visitor types
http://olddomain.com/terms.php he should be redirected to http://foobar.com/terms.php
I want that whatever the visitor types after the old domain name (like http://olddomain.com/privacy.php, http://olddomain.com/users.php, etc) they go to
http://foobar.com/ not to http://foobar.com/privacy.php etc.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^foobar\.com$ [NC]
RewriteRule .* http://foobar.com%{REQUEST_URI} [L,R=301]

Related

Redirect 301 and dynamic search pages generated

I did some redirects 301 from my old domain to a new domain on new host. It works well, but I'm having a lot of 404's on google webmasters generated by dynamic search pages. E.g.: www.newdomain.com/store/catalogsearch/result/index/?cat=60&dir=desc&limit=15&mod‌​e=grid&order=price&p=2&q=makita. But, I think this was crawled in the old domain, because of the searched words and are redirecting now to the new domain. Is there some generic rule to redirect all the others dynamic generated searchs to a specific page like home page on new domain without drop my ranking on google? On my new domain, dynamic search pages have been blocked in robots.txt. Is this way enough to not be crawled again? This is part of my redirect:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index)\.(html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.newdomain.com/$1 [R=301,NC]
RewriteRule ^store/?$ http://www.newdomain.com [L,NC,R=301]
RewriteRule ^store/folder/?$ http://www.newdomain.com.br/otherfolder/another-folder/ [L,NC,R=301]
RewriteRule ^store/folder1/folder2/?$ http://www.newdomain.com/folder-3/folder-4 [L,NC,R=301]
...
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,NE,L]
Thanks in advance for your help.
ok insert this rule just below RewriteEngine On to redirect these dynamic URLs to home page of new domain:
RewriteCond %{THE_REQUEST} /store/catalogsearch/result/index/ [NC]
RewriteRule ^ http://www.newdomain.com/? [L,R=301]

how to redirect domain name with directory

I have one project online and it on two diffrent domains 1)www.example.com and 2)www.example.co.in. now, I want to redirect
www.example.co.in/category/
to
www.example.com/ceramic+industry/
And I want to redirect it through .htaccess or from server. I tried from server that only redirect domain name not directory, and also tried from .htaccess where I can redirect only domain or only directory not when both domain and directory combine. My project in php. So, you can give advise if it's possible in php also.
Add following rule in .htaccess(placed at root of website www.example.co.in):
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
or use 302 for temporary redirects.
IF you also want to redirect whole .co.in website to .com THEN add next line(remember in .com website's .htaccess these two Rules should not be there.)
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
If for any reason you are bound to use same .htaccess on both site then use condition like this:
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
============================================================
Solution to your another query:
This can be done by PHP. As I guessed all URL after category/ is handled by one PHP page.
in that PHP page code this at top:
if(isset($_GET['company'])){
$company=$_GET['company'];
$companyNew=str_replace('_','+',$company);
if($company!=$companyNew){
header("location:/category/?company="+$companyNew,true,301);
//header("location:/ceramic+industry/?company="+$companyNew,true,301);
exit;
}
}
You can try this:
RewriteCond %{HTTP_HOST} ^your.first.domain$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://your.second.domain/$1 [R,L]
This will redirect, if your request contains existing directory name.
Answer to your comment. You can try this:
RewriteCond %{ENV:REDIRECT_FINISH} !^$
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} ^company=(.*)\_(.*)$
RewriteRule ^(.*)$ /$1?company=%1+%2 [R,L,E=FINISH:1]
First two lines allow you to prevent infinite loop redirect.

Rewrite a secondary domain to a subfolder

I bought two domains:
www.juridischehulponline.nl
www.onlinejuridischehulp.nl
And I'd like them to link to the subfolder of my hosting: www.zuidveste.eu/juridisch and the new domain has to be displayed as the new domain. So if I surf to www.juridischehulponline.nl I'd like to see: www.juridischehulponline.nl and not www.zuidveste.eu/juridisch.
However, if I rewrite and redirect the subfolder to the new domain, I get a: "too many redirects" error. Since it'll go to www.juridischehulponline.nl where an .htaccess is found which will redirect to www.juridsichehulponline.nl
and if I proxy the domain, my new domain keeps being displayed, however simple blogging systems, such as cutenews, won't work, because they see the absolute path as www.zuidveste.eu/juridisch. But due to the .htaccess, that part will be rewritten and the path of www.juridischehulponline.nl/index.php is given; which doesn't exist.
I've deleted the .htaccess in the /juridisch folder, but I don't know how this can be solved.
Can anyone help me?
Regards,
I wouldn't use proxy, but improve the .htaccess-structure: place only a .htaccess in your zuidveste.eu root:
Options -Indexes
RewriteEngine ON
Options +FollowSymLinks
#redirect non-www to www-domain
RewriteCond %{HTTP_HOST} ^juridischehulponline.nl [NC]
RewriteRule ^(.*)$ http://www.juridischehulponline.nl/$1 [R=301,L]
#redirect non-www to www-domain
RewriteCond %{HTTP_HOST} ^onlinejuridischehulp.nl [NC]
RewriteRule ^(.*)$ http://www.onlinejuridischehulp.nl/$1 [R=301,L]
#redirect non-www to www-domain
RewriteCond %{HTTP_HOST} ^zuidveste.eu [NC]
RewriteRule ^(.*)$ http://www.zuidveste.eu/$1 [R=301,L]
#rewrite other domains to the subfolder, keeping the attributes
RewriteCond %{HTTP_HOST} ^www.juridischehulponline.nl [NC,OR]
RewriteCond %{HTTP_HOST} ^www.onlinejuridischehulp.nl [NC]
RewriteCond %{REQUEST_URI} !^/juridisch/
RewriteRule ^(.*)$ /juridisch/$1 [QSA,L]
This code redirects the 3 non-www domains to their www-alternative, so the visitors will see that they are redirected. On the other hand, if they visit the 2 .nl-domains, the urls will be rewritten and the users will not see this change in their address-bar.
Nevertheless, take care for duplicate content in search engines. I recommend using a canonical-meta-tag, as explained here: http://moz.com/learn/seo/duplicate-content

htaccess domain redirect to newdomain

I have the domain www.oldsite.com and i set a redirect to forward to www.newsite.com/new
but when i google search old site all of its sub links go to www.newsite.com/new/sublink
How do i edit the htaccess file so that the sub links never pass though
RewriteCond %{HTTP_HOST} ^oldsite\.com*$ [OR]
RewriteCond %{HTTP_HOST} ^www\.oldsite\.com*$
RewriteRule ^/?$ "http\:\/\/www\.newsite\.com\/new" [R=301,L]
In short all links involved in oldsite.com should redirect to newsite.com/new
I read the below but it didnt seem to help to much.
htaccess: domain hosted on subdirectory
The below .htaccess should be place on the root folder of the old domain only:
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$ [NC]
RewriteRule ^ http://www.newsite.com/new [R=301,L]
It will redirect anything from the old domain to the new one, for example:
http://oldsite.com/someurl
Will go to:
http://www.newsite.com/new
No URLs from the old site will be passed down to the new site, they will all be sent to /new.
NOTE: make sure you do not have any additional .htaccess files in other folders of the old domain as those will set null the previous rules.
If you want to send only the main domain to the new one without any URLs within:
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$ [NC]
RewriteRule ^/?$ http://www.newsite.com/new [R=301,L]
A different way would be to handle all the URLs on the new site, which would be like the below .htaccess:
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$ [NC]
RewriteRule ^ http://www.newsite.com/new%{REQUEST_URI} [R=301,L]
It will redirect anything from the old domain to the new one, for example:
http://oldsite.com/someurl
Will go to:
http://www.newsite.com/new/someurl
If you do not wish the new to show up simple remove /new from the rule above.

Redirect using htaccess based on referrer

We only want users from a specific website to use our services. Is there a way to redirect all traffic that does not come from a specific referrer, to a website of our choosing, via htaccess?
Also, this is for the first page only. So if they get to our site, they're going to browse a new page, and their referrer for the new page would apparently be the site they are already on.
Thank you!
Try adding this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://the-ok-domain.com [NC]
RewriteRule ^/?first-page.html$ http://the-website-of-your-choosing.com/ [L,R]
You could also make it so you add your own domain to the referer check:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://the-ok-domain.com [NC]
RewriteCond %{HTTP_REFERER} !^http://your-domain.com [NC]
RewriteRule ^ http://the-website-of-your-choosing.com/ [L,R]
Then you can include all of your pages in the check.
Note that referers can be easily forged and any htaccess file using mod_rewrite in any of your subdirectories will supercede these rules (unless those htaccess files have the RewriteOptions inheret option set)
Didn't work for me, I've made this small change to redirect traffic from google:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^(.*)\.google\.(.*) [NC]
RewriteRule ^(.*)$ https://www.my-site.it/$1 [L,R]

Categories