简体   繁体   中英

Htaccess 301 redirect for domain only

I have a list of 301 Redirects like this inside htaccess:

Redirect 301 /oldpage1.php http://www.domain.com/newpage1.php
Redirect 301 /oldpage2.php http://www.domain.com/newpage2.php
Redirect 301 /oldpage3.php http://www.domain.com/newpage3.php
...

Now these pages shall be redirected only for a certain domain (there are other domains now pointing to the same location like www.domain.com).

Eg www.domain.com/oldpage1.php shall be redirected, but www.sub.domain.com/oldpage1.php not.

So whats the way to apply these redirects only for www.domain.com?

Thanks.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage1\.php/?$ newpage1.php [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage2\.php/?$ newpage2.php [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage3\.php/?$ newpage3.php [L,R=301]

PS: If you have several rules like above then it is much better to use RewriteMap .

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