gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:50:51 25 4
gpt4 key购买 nike

使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com