Bulk 301 redirect from htaccess

2019-05-31 13:48发布

I have spreadsheet which has column of 404 error link and its respective 301 redirects. for e.g)

404 error page                    301 redirect      

http://www.abc.com/1.php          http://www.abc/com/2.php
..............                    ............

The spreadsheet has around 1000+ links.

Is there any way in apache config or htaccess where can I handle bulk 301 redirects?

3条回答
Animai°情兽
2楼-- · 2019-05-31 14:40

The best way for a Bulk setting is the RewriteMap directive in mod-rewrite. Note that this will not work in a .htaccess file. You'll need to edit it in a real apache configuration file (and the good news is that will be really faster than a .htaccess dynamic configuration).

Check the txt: keyword first.

May be something like that:

RewriteMap myredirects txt:/path/to/my/txt/map.txt 
RewriteCond ${myredirects:$1} ^/.+
RewriteRule ^(.+)$ %0 [R=302]

When you'll get it working well transform the 302 in 301, and convert the txt file to an faster hashmap with the dbm: keyword (and a httxt2dbm command, but that's explained in the linked doc).

查看更多
地球回转人心会变
3楼-- · 2019-05-31 14:52

include_once this file in 404.php - make sure 404.php is configured to be called when there is 404 :

//ini_set('display_errors',2);
//ini_set('log_errors',0);
//ini_set('error_reporting',E_ALL);
$username = "dbuser";
$password = "dbpasswd";
$hostname = "db.hostname.com"; 
$database = "dbname";
$table    = "tablename";
$curl   = "http://". $_SERVER['HTTP_HOST'];

$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL". mysql_error());
mysql_select_db($database) or die('Could not select database' . $database );

$query = 'SELECT durl FROM urls where surl="' . $curl . '";';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

$durl = mysql_fetch_array($result);
if(isset ($durl) && $durl !=""){
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location:". $durl);
}
//else{
//  echo "404 Not Found ";
//}
// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($dbhandle);
?>

Table structure:

CREATE TABLE tablename (surl varchar(256),
durl varchar(256));

surl = Source Url

durl = Destination Url

查看更多
我想做一个坏孩纸
4楼-- · 2019-05-31 14:53

RewriteMap is indeed the way to go as stated by regilero. However, I couldn't understand how his code is supposed to work neither could I get it to work. One problem is that it assumes that there are no further rewrites (missing [L]) This does work:

In the apache config (does NOT work in .htaccess):

RewriteMap myredirects txt:/var/www/mysite/redirects.txt

In .htaccess (also works in apache config):

RewriteCond ${myredirects:%{REQUEST_URI}} .+
RewriteRule ^ ${myredirects:%{REQUEST_URI}} [L,R=301]

In redirects.txt (Note: Slashes are NOT optional)

/old /new

I'm not sure switching to dbm is worth the extra hassle. As stated on http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap , the lookup is cached in memory anyway.

For plain text and DBM format files the looked-up keys are cached in-core until the mtime of the mapfile changes or the server does a restart.

查看更多
登录 后发表回答