【问题标题】:Redirect everything that has /en/ to a specific page将所有具有 /en/ 的内容重定向到特定页面
【发布时间】:2016-09-28 09:28:45
【问题描述】:

我想要一个多语言网站,但我还没有完成英文版,所以如果 url 有 /en/,我想重定向到内容为“Soon”的特定页面。此页面将是 www.sitename.com/en/welcome

我在 Wordpress 中执行此操作,但我不知道 .htaccess 有那么好。这段代码实际上工作得很好,它检查语言集并相应地重定向,但问题是在访问我想去 www.sitename.com 的那个 url 之后,它仍然把我重定向到那个页面(可能是因为语言环境没有改变)。

   add_action("template_redirect", 'pl_redirect');
    function pl_redirect() {
      // if is english redirect to page id 193
      if (get_locale() == 'en_US' && !is_page(193)){    
        wp_redirect( 'http://www.sitename.com/en/welcome' );
        exit;               
      }
    }

我怎样才能正确地完成同样的事情? 我在 .htaccess 中尝试过,但它不起作用

Options +FollowSymLinks
RewriteEngine on
Redirect 301 ^/en/$ /en/welcome

这是由 .htaccess 中的 Wordpress 编写的

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

RewriteRule ^/en/$ /en/welcome [R=301]

</IfModule>

【问题讨论】:

  • RewriteRule ^/en/?$ /en/welcome [R=301,NC,L] - Redirect 处理不是 RegExps IIRC 的字符串,因此将是 Redirect 301 "/en" "/en/welcome"
  • 谢谢,但我不能让它工作:/我已经用 Wordpress 的内容编辑了代码
  • 我怀疑RewriteBase /web/ 正在使用扳手...
  • 站点临时设置在 /web... 所以 www.site.com/web/en 应该重定向到 www.site.com/web/en/welcome
  • 实际上 - 目录/web/en/ 真的存在吗?我认为这都是关于那个路由器的......

标签: php wordpress .htaccess redirect


【解决方案1】:

假设您实际上已将所有内容安装在名为 /web 的目录中 - 您将需要 2 个 .htaccess 文件:

/.htaccess(WordPress .htaccess 根文件)

# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On

# the base directory
RewriteBase /web/

# this means "don't rewrite /web/index.php"
RewriteRule ^index\.php$ - [L]

# this is a router any URL that's neither a file
# nor a directory will be routed to /web/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php [L]

</IfModule>

/web/.htaccess(你的重写)

RewriteEngine On
RewriteRule ^en/?$ /web/en/welcome [NC,L,R=302]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多