htaccess 301重定向到url而不传递子目录

时间:2016-12-08 23:24:59

标签: php wordpress .htaccess redirect mod-rewrite

我试图重定向这样的网址:

http://domain.com/word/baseball/cat/

到这样的网址:

http://domain.com/otherpage/

我使用的htaccess行是:

redirect 301 /word/baseball/cat/ http://domain.com/otherpage/

但重定向后生成的URL最终为:

http://domain.com/otherpage/baseball/cat/

我想将完整的网址重定向到新网址,而不会传递'棒球/猫/'子目录,所以我们希望最终得到URL:

http://domain.com/otherpage/

这适用于包含大约500个此类网址的网站,需要重定向到完全不同的网址。

我提前感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

如果您使用PHP执行此操作,可以的替代方式如下,但它可能不太有效:

header('Location: http://newurl.com');

还可以尝试:

RewriteEngine  on
RewriteRule    "/page1"  "http://example.com/page1"  [R]

答案 1 :(得分:0)

.htaccess文件放在DOCUMENT_ROOT的顶级目录中。该文件至少应包含这些重写指令:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . index.php
</IfModule>

现在来到网站的所有不存在的文件和目录网址都将转到index.php。在index.php内放置了一个路由机制。一个非常简单的看起来像这样:

<强>的index.php

$rurl = '';   
switch ($_SERVER['REQUEST_URI']) {
  case '/word/baseball/cat/': $rurl = '/otherpage1/'; break;
  case '/word/baseball/dog/': $rurl = '/otherpage2/'; break;
  case '/word/baseball/rat/': $rurl = '/otherpage3/'; break;
}
if (!empty($rurl)) {
  header('HTTP/1.1 301 Moved Permanently'); 
  header('Location: '.$rurl);
  exit();
}