Reputation: 33
I have issue with a strange url request to my site.. that may come from bot or other website .. i have researched all my code and link on web.. but not find and link like that the url they request not in correct order is:
/modules.php?file=article&name=News&sid=281737
but the correct one should be
/modules.php?name=News&file=article&sid=281737
in fact the php can handle both of those ... but i using static caching system , which makes two files on cache. Making lots of files is not good for web performance.
I need a php script that use $_SERVER['REQUEST_URI'] and regex to detect the wrong url then send it to correct url with 301 header
Thanks for any advice.
Upvotes: 1
Views: 493
Reputation: 34053
You could try something like this -
<?php
if (preg_match ('/file=article\&name=Name/', $_SERVER['REQUEST_URI'])) {
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: ' . str_replace('file=article&name=Name', 'name=Name&file=article', $_SERVER['REQUEST_URI']);
}
or for a more generic one
<?php
if (preg_match ('/file=(.*)\&name=(.*)/', $_SERVER['REQUEST_URI'], $matches)) {
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: ' . preg_replace('/file=(.*)&name=([^&]*)/', 'file=${1}\&name=${2}', $_SERVER['REQUEST_URI']));
}
Upvotes: 0
Reputation: 52372
Do it at the web server level. Since you tagged your question .htaccess
I'm assuming that's Apache.
RewriteEngine On
RewriteCond %{QUERY_STRING} file=(.*)&name=(.*)&sid=(.*)
RewriteRule modules.php modules.php?name=%2&file=%1&sid=%3 [R=301,L]
I haven't tested that, does it work?
Upvotes: 2