简体   繁体   中英

htaccess 301 redirect to url without passing subdirectories

I'm trying to redirect a URL like this:

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

To a URL like this:

http://domain.com/otherpage/

The htaccess line I'm using is:

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

But the resulting URL after the redirect ends up being:

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

I'd like to redirect the full URL to the new URL without passing the 'baseball/cat/' subdirectories, so that we hopefully end up with the URL:

http://domain.com/otherpage/

This is for a website with approximately 500 URLs like this that need to be redirected to entirely different URLs.

I appreciate any help in advance!

If you're doing this with PHP, an alternate way could be the following, however it may not be too effective:

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

Also try:

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

Put your .htaccess file at the top level directory, in your DOCUMENT_ROOT . The file should at least contain these rewrite instructions:

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

Now all non existing file and directory urls that come to the site will go to index.php . Within index.php put a routing mechanism. A very simple one would look like this:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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