来自 htaccess 的批量 301 重定向

发布于 2024-12-16 11:40:16 字数 314 浏览 2 评论 0原文

我有一个电子表格,其中包含 404 错误链接列及其各自的 301 重定向。 例如)

404 error page                    301 redirect      

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

电子表格有大约 1000 多个链接。

apache 配置或 htaccess 中有什么方法可以处理批量 301 重定向吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谢绝鈎搭 2024-12-23 11:40:16

批量设置的最佳方法是 mod 中的 RewriteMap 指令-改写。请注意,这在 .htaccess 文件中不起作用。您需要在真正的 apache 配置文件中编辑它(好消息是这比 .htaccess 动态配置要快)。

首先检查 txt: 关键字。

可能是这样的:

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

当你让它正常工作时,将 302 转换为 301,并使用 dbm: 关键字(和 httxt2dbmhttxt2dbm)将 txt 文件转换为更快的哈希映射。 code> 命令,但这在链接的文档中进行了解释)。

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).

提笔落墨 2024-12-23 11:40:16

正如 regilero 所说,RewriteMap 确实是要走的路。但是,我无法理解他的代码应该如何工作,也无法让它工作。一个问题是它假设没有进一步的重写(缺少 [L]) 这确实有效:

在 apache 配置中(在 .htaccess 中不起作用):

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

在 .htaccess 中(也适用于 apache 配置):

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

在 redirects.txt 中(注意:斜杠不是可选的)

/old /new

我不确定切换到 dbm 是否值得额外的麻烦。
http://httpd.apache.org/docs/2.2/所述mod/mod_rewrite.html#rewritemap ,查找无论如何都会缓存在内存中。

对于纯文本和 DBM 格式文件,查找到的键会被缓存
in-core 直到映射文件的 mtime 发生变化或服务器执行
重新启动。

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.

¢蛋碎的人ぎ生 2024-12-23 11:40:16

在 404.php 中包含_once此文件 - 确保 404.php 配置为在存在 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);
?>

表结构:

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

surl = 源 Url

durl = 目标 Url

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文