【问题标题】:htaccess redirect all subfolders/pages on one domain to matching subfolder on new domain (WordPress)htaccess 将一个域上的所有子文件夹/页面重定向到新域上的匹配子文件夹(WordPress)
【发布时间】:2015-09-18 09:38:26
【问题描述】:

我有一堆域都指向同一个站点,并且需要能够将 domainalias.com/folder/name 重定向到 website.com/folder/name。

我目前有以下 htaccess,它可以重定向根域,但是一旦我进入子目录或子页面,它就不会重定向:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

# Redirect all domains to website.co.uk
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^website.co.uk$ [NC]
RewriteRule ^(.*)$ http://website.co.uk/$1 [L,R=301]
</IfModule>

所以确认一下,我希望人们从http://anydomainalias.co.uk/any/sub/folder 重定向到http://website.co.uk/any/sub/folder。因为它是一个 WordPress 网站,所以我需要它来动态工作。我有 50 多个子域,因此希望避免为每个子域设置一个规则(因此有 !^website.co.uk 规则)。

谢谢。

【问题讨论】:

    标签: regex wordpress .htaccess mod-rewrite redirect


    【解决方案1】:

    在默认 WP 路由规则之前有重定向规则:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Redirect all domains to website.co.uk
    RewriteCond %{HTTP_HOST} !^website\.co\.uk$ [NC]
    RewriteRule ^(.*)$ http://website.co.uk/$1 [L,R=301]
    
    # BEGIN WordPress
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress
    
    </IfModule>
    

    请务必在清除浏览器缓存后对其进行测试。

    【讨论】:

    • 对不起,我应该重新表述为页面而不是文件夹。我需要将所有页面从域别名重定向到主域上的同一页面。我永远不会有完整的页面列表,因为该站点是一个动态 WordPress 站点,因此无法为每个页面制定规则。还有一个很好的 50 个别名域,所以我需要坚持 !^website.co.uk 规则来更好地处理所有事情。谢谢。
    • 啊!将规则移到 WP 之上就可以了。谢谢你:)
    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 2018-05-17
    • 1970-01-01
    • 2012-01-15
    • 2012-11-23
    相关资源
    最近更新 更多