繁体   English   中英

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

[英]htaccess 301 redirect to url without passing subdirectories

我正在尝试重定向这样的网址:

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重定向到新的URL 而不传递'baseball / cat /'子目录,因此我们希望最终得到这个URL:

http://domain.com/otherpage/

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

我提前感谢任何帮助!

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

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

还试试:

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

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

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

现在,访问该站点的所有非现有文件和目录URL都将转到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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM