如何设置.htaccess文件以排除子目录?

时间:2013-08-02 21:08:08

标签: .htaccess

我目前通过以下方式设置网站:

  • example.com是根域
  • .htaccess文件将所有对根URL的请求重定向到blog.example.com

这是因为blog子域是主域的镜像,将会向前发展。

问题是有第二个域:product.com指向example.com/product

在配置.htaccess文件后,所有传入请求都会映射到blog.example.com

这意味着转到product.com会产生blog.example.com/product,其中理想情况是example.com/product,所有其他传入请求都会重定向到blog.example.com

我的.htaccess文件的副本如下:

# Block access to the root site
RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]

# Whitelist specific areas of the root site
# e.g category slugs or page slugs you want to remain viewable
RewriteCond %{REQUEST_URI} !/blog.* [NC]
RewriteCond %{THE_REQUEST} !/blog.* [NC]

# Set like-to-like redirect URL for anything not whitelisted
RewriteRule (.*) http://blog\.example\.com/$1 [R=301,L]

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

我如何以下列方式构建规则:

  • product.com重定向到example.com/product
  • example.com重定向到blog.example.com

1 个答案:

答案 0 :(得分:1)

这应该有效:

RewriteEngine On

# redirect anything that is different than /blog and /product to blog.example.com
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !/blog.* [NC]
RewriteCond %{REQUEST_URI} !/product.* [NC]
RewriteRule ^(.*)$ http://blog.example.com/$1 [R=301,L]