1

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.

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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']));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.