301最简单的方法为小型网站移动到Wordpress



场景

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

  1. 将旧页面重定向到新的url结构
  2. 将所有www调用重定向到非www

挑战

然而,我想用来实现这一点

  1. Fewest每次调用可能重定向(最好只有1个)
  2. 具有有限的规则重复
  3. 具有一定的可读性(而不仅仅是一个内联Regex处理所有内容)
  4. 所有这些都是在htaccess单独编辑的

让我很难过的是,当它既是一个旧的url,又以www为前缀时,将重定向保持在最低限度。

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

更多详细信息

  • 我不是使用重写引擎的专家,所以这里可能缺少一个简单的解决方案
  • 旧网站的结构简单地使用/index.php?page=<pageName>
  • 新网站使用seo友好的URL/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->shiny-shoes)
  • 我自己计算机上的当前设置不允许我在本地测试它(与其他项目有很多冲突),所以目前我正在使用http://htaccess.madewithlove.be/.
  • htaccess文件的当前内容(在pastebin上,因为这里的代码一直被解释为其他内容)

我希望这不会与您在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.htaccess文件位于根目录中。

.2 GET值(以下示例中的"value")不会在请求的URL中进行修改。换句话说,"鞋子"在请求的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 />";
}
?>

最新更新