簡體   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