php - 一些重定向翻译不正确,而大多数都按预期工作

标签 php mysql apache redirect

使用 Apache、MySQL 和 PHP,我注意到 URL 重定向的奇怪行为。

我们的数据库条目是

src = 'pfe/pdf/03-b-06.pdf'
dst = 'https://site2.com/communities/policyprocedure/pfe/Active/03-b-06.pdf'

重定向结果正确翻译:

'http://site1.com/pfe/pdf/03-b-06.pdf'

决议:

'https://site2.com/communities/policyprocedure/pfe/Active/03-b-06.pdf'

但是,这个不能正确解析:

src = 'employee/jsp/common/generic.jsp?id=22910'
dst = 'https://site2.com/communities/policyprocedure/pfe/intranet-pages/22-urology'

重定向翻译为:

'http://site1.com/employee/jsp/common/generic.jsp?id=22910' 

响应于:

'https://site2.com/communities/policyprocedure/pfe/intranet-pages/22.-urology'

我们有数以千计的使用数字、连字符和字母字符(例如/03)的 URL 可以工作,但几千个中只有十几个不能工作。

这可能与目的地及其 URL 编码有关吗?我已确定它不是 SQL 插入/更新字符串中的字符集或字符编码。

最佳答案

索引 php:

<?php
// Pull in the database config
require_once dirname(__FILE__) . "/config.php";
require_once dirname(__FILE__) . "/functions.php";

// Turn off error reporting
error_reporting(0);

// Get the requested URL
$src = htmlspecialchars($_GET['src']);

// Handle the empty src
if (empty($src)) {
  header('Location: ' . $at);
  exit;
}

// Open a DB connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Stop if the DB connection fails
if ($conn->connect_error) {
  die('Connection failed: ' . $conn->connect_error);
}

// Query the database
$sql = "SELECT dst FROM map WHERE src = '" . $src . "'";
$result = $conn->query($sql);
$conn->close();

// Act on the results
if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    if (!getResult($row['dst'])) {
      fourOhFourCapture($sp,$row['dst']);
    }
    header('Location: ' . $row['dst']);

  }

配置.php:

<?php

$servername = 'servername';
$username = 'username';
$password = 'password';
$dbname = 'database';

$file = '/home/svc_account/404_results.txt';

$at = 'https://site1.cpm/';
$sp = 'https://site2.com/';

函数 php:

<?php

function urlExist($url)
{
  $handle = curl_init($url);

  if (false === $handle) {
    return false;
  }

  curl_setopt($handle, CURLOPT_HEADER, false);
  curl_setopt($handle, CURLOPT_FAILONERROR, true);
  curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") );
  curl_setopt($handle, CURLOPT_NOBODY, true);
  curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
  $connectable = curl_exec($handle);
  curl_close($handle);
  return $connectable;
}

function fourOhFour($url,$req)
{
  header('Location: ' . $url . '?requestUrl=' . $req);
}

function fourOhFourCapture($url,$src)
{
  global $file;
   file_put_contents($file, $url.$src, FILE_APPEND | LOCK_EX);
}

/**
 * getResult - accepts a url, uses curl to check the status, and returns false if the code is 404, and true for any
 * other result
 * @param $url
 * @return bool
 */
function getResult($url) {

  $handle = curl_init($url);
  curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

  /* Get the HTML or whatever is linked in $url. */
  $response = curl_exec($handle);

  /* Check for 404 (file not found). */
  $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  curl_close($handle);

  if($httpCode == 404) {
    return false;
  } else {
    return true;
  }

}

apache 重写:

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteRule ^$ https://site2.com/ [L]

  RewriteRule ^/index\.php$ - [L]
  RewriteRule ^/lbactive\.html$ - [L]
  RewriteRule ^/robots\.txt$ - [L]

  RewriteCond %{REQUEST_URI} ^/dept/department\.jsp [OR]
  RewriteCond %{REQUEST_URI} ^/common/generic\.jsp
  RewriteRule .* %{REQUEST_URI} [E=IS_LEGACY_OID:TRUE]

  RewriteMap legacy_oid_map txt:/var/vhost/site1/data/rewrites_legacy_oid.txt

  RewriteCond %{ENV:IS_LEGACY_OID} ^TRUE$
  RewriteCond %{QUERY_STRING} id=([0-9]+)
  RewriteRule .* ${legacy_oid_map:%1}? [R=301,L]

  RewriteRule ^/site1/(.*)$ http://%{SERVER_NAME}/$1? [R=301,L]

  RewriteRule ^/(.*)$ /index.php?src=$1 [R=301,L]
</IfModule>

关于php - 一些重定向翻译不正确,而大多数都按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47336436/

相关文章:

javascript - 如何在脚本计数后立即使用 AJAX 从数据库中查看数据?

php - 应用程序中的夜间模式选项,可自动降低屏幕亮度。它会使用一些 php 或 html 或 css 代码来锻炼吗?

PHP 和 MySQL 单选按钮名称 = PHP ECHO

mysql - 您的 SQL 语法有误...创建触发器时出现错误

apache - RewriteRule (contact.php -> contact) + 404 重定向总是给我一个 404 页面

php - 下载文件时 index.php 未打开和运行

php - 程序 : Unfriendly Numbers 中的未识别错​​误

php - MySQL Group By Product Id 但不是有序列号的产品

python - Pandas 将数据框写入带有附加的 Parquet 格式

php - MySQL查询: LIKE %$var% but not when $var empty