Redirecting subdomain and subfolder to root domain

时间:2016-07-11 21:20:25

标签: .htaccess redirect subdomain

I think I'am having problems with SEO because I'am using 2 domains in the same server.

In the root folder I have www.dinastiabus.pt and in root/viaescola.pt I have www.viaescola.pt.

Dinastiabus is well indexed by google but Viaescola is not.

What I think I need to do is to redirect with .htaccess:

  • viaescola.dinastiabus.pt to viaescola.pt
  • and dinastiabus.pt/viaescola.pt/ to viaescola.pt

Even if it's not the problem I would like to do it anyway. I also would like the link to always have www.

This is what I have at this moment (but although it doesn't even have everything I want it is simply not working!):

RewriteEngine on

# Redirect to domain to www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# 301 Redirect URLs.
RedirectMatch 301 ^/www\.dinastiabus\.pt/viaescola\.pt/(.*)$ /www.viaescola.pt/$1
RedirectMatch 301 ^/dinastiabus\.pt/viaescola\.pt/(.*)$ /www.viaescola.pt/$1

# Prevent viewing of htaccess file.
<Files .htaccess>
order allow,deny
deny from all
</Files>

# Prevent directory listings
Options All -Indexes

1 个答案:

答案 0 :(得分:1)

These two RewriteRules should work:

RewriteCond %{HTTP_HOST} ^(www\.)?viaescola\.dinastiabus\.pt$ [NC]
RewriteRule ^ http://www.viaescola.pt%{REQUEST_URI} [R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?dinastiabus.pt$ [NC]
RewriteRule ^viaescola.pt(.*) http://www.viaescola.pt$1 [R=301]

This will take any URLs starting with

  • http://viaescola.dinastiabus.pt
  • http://www.viaescola.dinastiabus.pt/
  • http://dinastiabus.pt/viaescola.pt/
  • http://www.dinastiabus.pt/viaescola.pt/

and convert them to

  • http://www.viaescola.pt