2013-08-02 52 views
0

我現在有下列方式網站設置:如何設置.htaccess文件以排除子目錄?

  • example.com是根域blog.example.com

  • .htaccess文件重定向到根URL的所有請求,這是因爲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

    這應該工作:

    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] 
    
    +0

    正是我需要的 - 謝謝! – Tom

    +0

    @Tom很高興它爲你工作 – Prix

    相關問題