How to redirect 301 by rewriting URL
When you running a website, sometimes you will have visit requirement, like as use http://1.abc.com to visit http://www.abc.com/1, and you need to redirect setting of website visiting. The most efficient method in our platform is rewrite the URL. Below are some samples:
1.
Redirect domain.com to www.domain.com
Rewrite Engine On
Rewrite Cond %{HTTP_HOST} !^www.domain.com$ [NC]
Rewrite Rule ^(.*)$ http://www.domain.com/$1 [L,R=301]
2.
Redirect www.domain.com to domain.com
Rewrite Engine On
Rewrite Cond %{HTTP_HOST} !^domain.com$ [NC]
Rewrite Rule ^(.*)$ http://domain.com/$1 [L,R=301]
3.
Redirect olddomain.com to www.newdomain.com
Rewrite Engine On
Rewrite Cond %{HTTP_HOST} !olddomain.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L,R=301]
4.
Redirect olddomain.com to newdomain.com
Rewrite Engine On
Rewrite Base /
Rewrite Cond %{HTTP_HOST} !olddomain.com$ [NC]
Rewrite Rule ^(.*)$ http://newdomain.com/$1 [L,R=301]
5.
Redirect domain.com/file/file.php to otherdomain.com/otherfile/other.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$
Suggested Reading:
- What Operating System should I install on my server?(Views: 2099)
- VPS small memory optimization suggestions (LNMP + WordPress environment)(Views: 3585)
- The differences among Fedora, RedHat, CentOS and other Linux based OS (Views: 2406)
- Arp attack phenomenon and solution(Views: 3294)
- Commonly used commands in Linux(Views: 2162)