Link to home
Start Free TrialLog in
Avatar of Valleriani
VallerianiFlag for Sweden

asked on

.htaccess manipulation of a link? (From name: StringA to StringB

Hello,

I have the following link, for example

http://test.com/index.php?browseto=campaignArea&page=1


Etc, very simple. What I want is .htaccess to make a certain word go to the same area. For example

http://test.com/index.php?ulink=broadcastArea&page=1
would be the same as
http://test.com/index.php?ulink=campaignArea&page=1

.. There is a problem with just changing it because of the structure the files were created in,. 'campaignArea' is actually a value that represents a mySQL value it searches for, and it's used in 'several' files statically. The simple idea I had instead of having to adjust more then 100 files would be to make 'broadcastArea' go to the same location as campaignArea if its in the link.

The thing is, I don't want the 'name' to be different. I can obviously make broadcastArea redirect to campaignArea first, but I want it so that the user sees:

'http://test.com/index.php?ulink=broadcastArea&page=1' still, but the server would see

'http://test.com/index.php?ulink=campaignArea&page=1' if that makes sense, which I hope .htaccess can actually do.

broadcastArea actually goes no where, but campaignArea would work, is the reason why, in a quick sense.


So to short form it, replace part of a string with another string, but the link should stay the same in the users browser, link manipulation/prettifying. Is this possible?
Avatar of Valleriani
Valleriani
Flag of Sweden image

ASKER

I've tried something like

RewriteRule (.+)+(test)+(.+) $1test2$2 [R=301,L]

Which should change test to test2 anywhere, but  no luck.
Certainly it is possible and I would think that you would not want to directly rely on the contents of the request (URL arguments) to build the query string.  That smells like a security exposure to me, or at least a brittle design.

Here is how I would do it... Forget about URL rewriting and instead use a translation table that makes an intelligent interpretation of the contents of the $_GET['ulink'] parameter.  This has two effects.  First, it provides automatic filtering of the external (tainted) data.  Second, it makes for a design that decouples your data base queries from external data.

See http://www.laprbass.com/RAY_switch_example.php
<?php // RAY_switch_example.php
error_reporting(E_ALL);
echo "<pre>";


// DEMONSTRATE THE USE OF SWITCH / CASE CONDITIONAL LOGIC
// MAN PAGE HERE: http://php.net/manual/en/control-structures.switch.php
// NOTE THIS: "The switch statement is similar to a series of IF statements ON THE SAME EXPRESSION."


// IF ANYTHING POSTED
if (!empty($_GET["i"]))
{
    // GET THE POSTED DATA IN A LOCAL VAR
    $i = trim(strtoupper($_GET["i"]));

    // SWITCH ON THE BASIS OF THE CONTENTS OF OUR LOCAL VAR
    switch ($i)
    {
        case "A" :
        case "B" : echo "YOU ENTERED EITHER 'A' OR 'B' ";
                   $sql = "SELECT thing FROM table WHERE thing = 'AAA' OR thing = 'BBB'";
                   break;

        case "C" : echo "YOU ENTERED 'C' ";
                   $sql = "SELECT thing FROM table WHERE thing = 'CCC'";
                   break;

        default  : echo "YOU ENTERED NONE OF A, B OR C ";
                   $sql = "SELECT thing FROM table WHERE 1=1";
    }

    echo PHP_EOL;
    echo "THE RESOLVED QUERY SAYS: $sql";
}

// CREATE THE FORM FOR INPUT (OR JUST USE THE URL)
$form = <<<FORM
<form>
ENTER 'A' 'B' OR 'C'
<input name="i" />
<input type="submit" />
</form>
FORM;
echo $form;

Open in new window

Yes, it pretty much is a 'hack' in a sense, I agree. The script itself is such a mess and just needs a minor name fix. The thing is though it's all over the place, it's wouldn't be one simple edit and its a bit of a problem because of it, specially because its kind of its own 'php' module in a sense.

The link itself actually runs with the script, so

http://test.com/campaignID=1 for example would mean that campaignID is an actual name of the mysql query. When grabbing from the system you actually don't call mysql directly, it's a 'form builder'.

At the moment, it's defined as campaignID, but since they changed the name to broadcastID, they want that simple value changed. The problem is, campaignID would have to be changed internally, in the MYSQL database because of how the form builder works (it uses it to save, load, etc.) Which means you then would have to modify a ton of files, because the 'campaign' itself is the main base for this system, so any crons, most of the php scripts, would need to be changed. Whenever I just tell it in the one script that campaignID = broadcastID, attempting to load the script gives a 'invalid column' error because of how the form builder works.

The form builder is a honest mess, over 300KB of coding. I did not actually do it, but they seem to want a 'fix' instantly and it's not a quick job to do if I was to have to do it file per file, so it's the reason why for this one area I simply wanted it to think that campaignID = broadcastID in the link,  without having to mess with a hundred files.


Sorry if I can't explain this well, sometimes have a hard time speaking about it.
Yeah, I get it.  That's what I meant by a brittle design - even a modest change like this one will break lots of things.  Suggest you leave the question open for a while; maybe someone in the Apache Zone will pick up on it and give you a rewrite rule that can translate the URL.
Yeah I agree, it was rather a pain, and they don't understand the concept that a simple thing like that breaks everything/expect a change fast, bit of a pain. Hopefully someone does have an idea.

Thanks though for the reply!
Thank you,

So again, I have a link:

http://test.com/index.php?ulink=campaignArea&page=1

For example, and I want

http://test.com/index.php?ulink=broadcastArea&page=1


to go to the first link. So 'broadcastArea' would get changed to 'campaignArea', using .htaccess rewrite.

Thanks!
RewriteCond %{REQUEST_URI} !/ulink=campaignArea/
RewriteCond %{REQUEST_URI} /ulink=broadcastArea/
RewriteRule (.*) /index.php?ulink=campaignArea&page=1
Apologies, but that looks like it would only work for 'page=1'. needs to be dynamic, aka any sort of variables added after it. can be page10, maybe another & variable. I could get them all if needed, but the variables do change. I believe in the link they have about 4-5 of them.
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.