简体   繁体   中英

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.

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.

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;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM