【问题标题】:htaccess redirect from site built in a subdirectory从子目录中构建的站点的 htaccess 重定向
【发布时间】:2014-02-23 07:56:21
【问题描述】:

这似乎是一种罕见的情况,因为到目前为止我还没有找到关于它的线程。

我在子目录中建立了一个 wordpress 站点(即 example.com/subdir/ 是主页)。

我现在正在同一个域上构建一个新站点来替换 example.com/subdir/,它将位于根目录中的 example.com。

我需要 301 将 example.com/subdir/folder1/ 下的所有页面重定向到 example.com/newfolder/ 下的一个页面

我没有很多 htaccess 经验,但我希望以下 htaccess 文件能够工作(我将它放在 example.com/subdir/ 目录中)。但是,它仍然允许我访问 example.com/subdir/folder1/ 及其下面的所有页面,而无需将我重定向到 example.com/newfolder/

我做错了什么?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdir/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdir/index.php [L]

RedirectMatch 301 ^/folder1(.*)$ http://example.com/newfolder/
</IfModule>

我也尝试使用 Redirect 301 命令而不是 RedirectMatch 301,并且没有骰子。

【问题讨论】:

    标签: wordpress .htaccess mod-rewrite redirect url-redirection


    【解决方案1】:

    在其他 WP 规则之前有 301 条规则:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /subdir/
    
    RewriteRule ^folder1(.*)$ http://example.com/newfolder/ [L,R=301]
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /subdir/index.php [L]
    
    </IfModule>
    

    【讨论】:

    • 谢谢,做到了。处理一对一重定向的最佳方法是什么?如果我需要 example.com/subdir/page3/ 重定向到 example.com/page6/ 我会使用 RewriteRule page3/ example.com/page6 吗?
    猜你喜欢
    • 2017-05-06
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多