Last active
April 27, 2017 10:19
-
-
Save folletto/f1821b8f0a7ad6b19b5018e82d25b39d to your computer and use it in GitHub Desktop.
Script to load a file listing URL pairs and checking it redirects from the first to the second
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This script loads a list of urls from "curl-tester.txt" file to check for 301 redirects. | |
// The file format is two URL (source, 301 redirection destination) separated by space. | |
$file_list = 'curl-tester.txt'; | |
function get_urls_list_from_file( $file ) { | |
//$content = file_get_contents( $file ); | |
$out = array(); | |
$array = file( $file ); | |
foreach( $array as $line ) { | |
if ( ! preg_match( '|^\s*//|', $line ) ) { // Not a comment | |
$line = preg_replace( '#\n|\r#', "", $line ); | |
if ( ! $line == "" ) { | |
$out[] = preg_split( '|\s+|', $line, 2, PREG_SPLIT_NO_EMPTY ); | |
} | |
} | |
} | |
return $out; | |
} | |
function file_get_contents_with_header( $url ) { | |
$curl = curl_init( $url ); | |
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $curl, CURLOPT_HEADER, true ); | |
// Request | |
$response = curl_exec( $curl ); | |
if ( curl_error( $curl ) ) die( curl_error( $curl ) ); | |
// Parse | |
$header_size = curl_getinfo( $curl, CURLINFO_HEADER_SIZE ); | |
$header = substr( $response, 0, $header_size ); | |
$body = substr( $response, $header_size ); | |
$status = curl_getinfo( $curl, CURLINFO_HTTP_CODE ); | |
curl_close( $curl ); | |
return array( | |
'status' => $status, | |
'header' => $header, | |
'body' => $body | |
); | |
} | |
// Run | |
$urls = get_urls_list_from_file( $file_list ); | |
foreach ($urls as $url_pair ) { | |
$url_to_check = $url_pair[0]; | |
$url_to_match = $url_pair[1]; | |
$response = file_get_contents_with_header( $url_to_check ); | |
$matches = array(); | |
if ( preg_match( '|Location:\s+([^\s\n\r]+)|', $response['header'], $matches ) ) { | |
$location_url = $matches[1]; | |
if ( strpos( $location_url, $url_to_match ) !== false ) { | |
echo "\nYES-301," . $url_to_check . "," . $location_url . ""; | |
} else { | |
echo "\nNO-MATCH," . $url_to_check . "," . $url_to_match . "," . $location_url . ""; | |
//print_r( $matches ); | |
} | |
} else { | |
echo "\nNO-301," . $url_to_check . ""; | |
} | |
//echo $response['header']; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://theme.wordpress.com/calypso-redirect-test/thetest https://wordpress.com/themes?calypso-redirect-test=thetest | |
// Root | |
https://themes.wordpress.com/themes https://theme.wordpress.com/ | |
https://theme.wordpress.com/themes https://theme.wordpress.com | |
https://theme.wordpress.com https://wordpress.com/themes | |
https://theme.wordpress.com/ https://wordpress.com/themes | |
// Shops | |
https://theme.wordpress.com/themes/by/automattic/page/2 https://wordpress.com/themes/premium?s=automattic | |
https://theme.wordpress.com/themes/bari/support https://wordpress.com/theme/bari/support | |
// Themes | |
https://theme.wordpress.com/themes/chateau/ https://wordpress.com/theme/chateau | |
// Retired themes | |
https://theme.wordpress.com/themes/ecto https://wordpress.com/theme/ecto | |
// Features | |
https://theme.wordpress.com/themes/features/black+wordads/?sort=free&theme-search=bla https://wordpress.com/themes/free/filter/black+wordads?s=bla | |
https://theme.wordpress.com/themes/sort/free/ https://wordpress.com/themes/free | |
https://theme.wordpress.com/themes/sort/newest https://wordpress.com/themes | |
https://theme.wordpress.com/themes/search/blabla https://wordpress.com/themes?s=blabla | |
https://theme.wordpress.com/themes/features/black+wordads/?sort=free https://wordpress.com/themes/free/filter/black+wordads | |
https://theme.wordpress.com/themes/features/black+wordads/?sort=free&theme-search=blabla https://wordpress.com/themes/free/filter/black+wordads?s=blabla | |
https://theme.wordpress.com/themes/features/fixed-width /themes/features/fixed-layout | |
https://theme.wordpress.com/themes/layouts/fixed-layout https://wordpress.com/themes/filter/fixed-layout | |
// Landing pages | |
https://theme.wordpress.com/customize https://wordpress.com/easy/ | |
// Custom Sites | |
https://theme.wordpress.com/credits/littlewolfblog.com/ https://wordpress.com/themes | |
// Themes with name changed | |
https://theme.wordpress.com/themes/eventbrite-multi /themes/eventbrite-venue | |
https://theme.wordpress.com/themes/eventbrite-single /themes/eventbrite-event | |
https://theme.wordpress.com/themes/photography-classic /themes/photography | |
https://theme.wordpress.com/themes/photography-classic/support /themes/photography/support | |
https://theme.wordpress.com/themes/largo /themes/journalistic | |
https://theme.wordpress.com/themes/largo/support /themes/journalistic/support | |
// Catch all | |
https://theme.wordpress.com/little/brown/cat https://wordpress.com/themes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment