最简单的301小网站方法转移到Wordpress

时间:2012-11-19 21:12:45

标签: apache .htaccess http-status-code-301

方案

我正在审核已迁移到基于Wordpress的设置的网站,但无法访问Wordpress中的所有管理工具(它是外部托管/托管的),但我确实可以访问htaccess文件。现在,我需要做两件事:

  1. 将旧网页重定向到新的网址结构。
  2. 将所有www来电重定向至非www。
  3. 挑战

    但我想通过以下方式实现这一目标:

    1. 每次通话可能的重定向最少(最好只有1次)。
    2. 有限的规则重复。
    3. 有点可读(不只是一个内联Regex处理它)。
    4. 仅在htaccess中编辑。
    5. 让我困难的是,当它既是旧网址又以www为前缀时,将重定向保持在最低限度。

      我的下一个最大的问题是,我想使用重写映射,但似乎你必须在外部加载一些东西,而不是在htaccess文件中定义一个我更喜欢的字典。

      更多详情

      • 我不是使用重写引擎的专家,所以我可能会错过一个简单的解决方案。
      • 旧网站的结构只有/index.php?page=<pageName>
      • 新网站使用了友好的网址/a-new-url-example/
      • 两个例子可能是:
        1. mydomain.com/index.php?page=bootsmydomain.com/boots/
        2. www.mydomain.com/index.php?page=shoesmydomain.com/shiny-shoes/ (需要某种地图来处理shoes - &gt; shiny-shoes
      • 我自己计算机上的当前设置不允许我在本地测试它(与其他项目有很多冲突),所以目前我正在使用http://htaccess.madewithlove.be/测试设置。
      • Current content of the htaccess file(在pastebin上,因为代码在这里被解释为其他东西)。

2 个答案:

答案 0 :(得分:2)

我希望这不会与你在htaccess中的现有规则发生冲突,正如你提供的那样。

#exceptions first
RewriteCond %{QUERY_STRING} ^page=shoes3$ [NC]
RewriteRule ^index\.php$ /shiny-shoes/? [R=302]

#urls that directly map to the new url scheme
RewriteCond %{QUERY_STRING} ^page=(.*)$ [NC]
RewriteRule ^index\.php$ /%1/? [R=302]

#note the absence of the L flag in the above rules.
# from apache docs: the [R] flag prepends http://thishost[:thisport] to the URI, but then passes this on to the next rule in the ruleset

# no-www (make sure this is the last rule)
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^http://[^/]+/(.*)$ http://%1/$1 [R=302,L]

答案 1 :(得分:0)

以下是一个例子:

<强>条件:

带有这些规则的

.1 .htaccess文件位于根目录。

.2未在请求的URL中修改GET值(以下示例中的“值”)。换句话说,“鞋子”在请求的URL中是相同的,而不是问题中的“闪亮鞋子”。它是可行的,但需要在模式中包含别名列表,或者每个项目都有不同的规则。

.3根目录中的index.php脚本必须能够处理所有可能的值,并在未找到匹配项时加载正常页面(如果有)。

    RewriteEngine on
    Options +FollowSymLinks

    #Redirect from http://www.mydomain.com to http://mydomain.com
    RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
    RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]

    #mydomain.com/value/ to  mydomain.com/index.php?page=value
    RewriteRule ^(.*)?/$ index.php?page=$1 [L]

要测试此示例,请在根目录中的index.php中包含以下唯一代码:

    <?php
    if ( $_GET[ 'page' ] == 'value' ) { // Change "value" accordingly 
      echo "Processing item <br /><br />";
    }
    else {
      echo "Loading normal page<br /><br />";
    }
    ?>