301 Redirect dynamic URL matching expression - please help

Hi guys. I’ve spent the last few hours trying to get my head around this problem. I’ve got a site using some translation software and it works for the most part but it cannot work in the checkout stages so I want to redirect the url it’s creating www.mysite.com/nl/index.php?etc to www.mysite.com/index.php?etc

The URL I get atm is http://www.mysite.com/ja/index.php?p...ed=1&Itemid=65

and I would love to get http://www.mysite.com/index.php?page...ed=1&Itemid=65

I’ve been trying different combinations of things but I simply don’t have the experience with mod rewrite, if anyone could point me in the right direction, I’d greatly appreciate it. Thanks for your time. Ant.

Ant (natastna2),

First, WELCOME to SitePoint’s Apache forum!

Second, it would help if you’d read the mod_rewrite tutorial linked in my signature. It develops the regex you need to convert from one server/path/file to another and then provides a lot of explained examples.

Third, I’ll guess that you entered full links but the forum software shortened them and that information is lost - not that it seems that it would make much difference. Wrapping code (and URLs) in [noparse]

...

[/noparse] would preserve EVERYTHING between the brackets.

Fourth, “specificity” is important in letting someone else know what you’re attempting so let me give you a start:

nl/index.php?{query_string} => index.php?{query_string} is merely a directory change (code in the tutorial) and the query string is NOT affected by the redirection UNLESS you add/modify the query string with the redirection. The ja directory would get the same treatment.

Please give it a try then share your code and level of success. Don’t worry, we’ll get you where you want (but I treat this board as a learning tool, not for free coding).

Regards,

DK

Hi, thanks for the reply. So far I’ve got as far as

RewriteCond %{QUERY_STRING} page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65
RewriteRule ^index\\.php$ http://www.seed-city.com/index.php?
page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1 [R=301]

Now I know that works and picks up the query string and redirects the URL to the same URL. It’s just I want to specify that there has to be a nl/ ja/ de/ preceding the index.php? and I’m no sure how to do that, even after reading the tutorial. I don’t understand what you mean by “give that a try”. Are you saying that I can use:

RewriteRule ^nl/index.php?{query_string} => index.php?{query_string}[R=301]

How do I specify the query_string to use? would this work:
RewriteRule ^nl/index.php?{page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65} => index.php?{page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65}[R=301]

Thanks for your time.

Ant,

How do you know the redirection worked if it was to the same URI?

Okay,


RewriteRule ^nl/index.php?{query_string} => index.php?{query_string}[R=301]

is obvious incorrect (for several reasons):

  1. A RewriteRule can NOT examine anything other than the {REQUEST_URI} so any attempt to do so will result in an error (incorrect regex): The ? is a metacharacter which means “zero or one” of the preceding character and curly braces are not allowed but, if they were, you’d have to have {query_string} in your URI - I’d used {query_string} as a place holder.

  2. RewriteRule format is “RewriteRule” followed by a single space followed by “regex” followed by a single space followed by “the redirection” optionally followed by a single space and square bracketed flags (without ANY MORE SPACES). Obviously, do NOT include the quotes!

  3. As explained above, you do not need to concern yourself with the query string if you mean to retain it. Otherwise, you MUST use a RewriteCond statement to view the {QUERY_STRING} variable. If your redirection was to change the query string in any way, you would likely use this to capture values for the redirection.

Try it meant to use:

RewriteRule ^nl/index\\.php$ index.php [R=301,L]

Oh, you were SO close (but didn’t read the tutorial with comprehension). Questions?

Regards,

DK

Thanks for the quick reply :slight_smile:

Wouldn’t

RewriteRule ^nl/index\php$ index.php [R=301,L]

Not work, because it would direct any instance of /nl/index.php followed by any string to the second URL. I only want the URL redirecting if the string is

page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65

Oh and I know the aforementioned is working because I can change the secondary URL to google and it redirects.

Ant,

IF you want the redirection only for Itemid=65 (the entire query string - even the duplicated &Itemid=65), then, by all means, precede the RewriteRule with

RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65
$

which will REQUIRE an EXACT match.

Regards,

DK

OK, So I’ve got

RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65$
RewriteRule ^nl/index\\php$ index.php [R=301]

and I get Internal Server Error so I tried

RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65$
RewriteRule ^nl/index\\.php$ index.php [R=301]

with a fullstop before php and no server error but no redirection :frowning:

OK, so the closest I’ve got is

RewriteCond %{HTTP_HOST} ^www.seed-city.com
RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65
RewriteRule ^(.*)$ http://www.seed-city.com/nl/index.php?page=checkout.index&option=com_virtuemart&Itemid=65 [R=301]

This is completely perfect, the only problem is it’s in reverse. I just can’t figure that part out :slight_smile: I need that URL with the nl/ prefix to redirect to the URL without the prefix. Any ideas would be much appreciated. Thanks all.

Ant,

Okay, that missing dot character was my mistake but you’ve corrected it and all should be well with.

RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65$
RewriteRule ^nl/index\\.php$ index.php [R=301]

And that is changing the specification on us! For shame! :lol: Okay, that’s a noobie mistake thinking that mod_rewrite will redirect to the NEW format.

If you’ll tell us what you REALLY want, we can help you get through the mod_rewrite. Of course, you’ll need to be sure that all the pieces are in the specification so the redirection makes sense.

Regards,

DK

Hi,

RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65$
RewriteRule ^nl/index\\.php$ index.php [R=301]

doesn’t work for me at all. What I want is for this URL:

http://www.seed-city.com/nl/index.php?page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65

to become:

http://www.seed-city.com/index.php?page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65

As I say, I can get the reverse to happen fine with:

RewriteCond %{HTTP_HOST} ^www.seed-city.com
RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65
RewriteRule ^(.*)$ http://www.seed-city.com/nl/index.php?page=checkout.index&option=com_virtuemart&Itemid=65 [R=301]

But I want the URL to change from http://www.seed-city.com/nl/index.php?page=checkout.index&option=com_virtuemart&Itemid=65 to http://www.seed-city.com/index.php?page=checkout.index&option=com_virtuemart&Itemid=65 not the other way around.

Ant,

The code above used your “weird” query string (Itemid=65&redirected=1&Itemid=54) does exactly that! It matches your query string then, if the URI is index.php, performs the redirection to eliminate the nl subdirectory.

Your code, however,

RewriteCond %{HTTP_HOST} ^www.seed-city.com
[indent]AGAIN, ESCAPE the dot characters, end the string and use the No Case flag![/indent]
RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65
[indent]Only one Itemid=65 and no redirection=1?[/indent]
RewriteRule ^[COLOR="Red"](.*)[/COLOR]$[COLOR="Gray"] http://www.seed-city.com/[/COLOR]nl/index.php?page=checkout.index&option=com_virtuemart&Itemid=65 [R=301]
[indent]Redirect EVERYTHING?  Redirect NOTHING?

Making an external (absolute) redirect? Why?  That's only necessary if not at the same {HTTP_HOST} (which you've required in the RewriteCond).

Changing the query string, too?  PLEASE check your specification as the query string you've insisted upon is the crux of this matter.[/indent]

UNLESS you’re trying to redirect TO the nl subdirectory?

But redirecting TO nl/ is NOT what you’ve requested, is it?

Regards,

DK

Hi, thanks for your reply.

I’ve tried

RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65$
RewriteRule ^nl/index\\.php$ index.php [R=301]

But it does not appear to be working.

The problem seems to be that it’s not picking up the nl/index part of the rewrite rule. If I remove nl/ the redirection will work but with the nl/ in place it’s not picking it up. Any help would be much appreciated. Thanks, Ant. Could it be a problem that the URL is dynamically generated and there is no existing nl folder?

Ant,

Would you PLEASE address the duplicated Itemid=65 in your query string? Is this whole thing generated? Generated as “page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65”? I feel that we’re not going anywhere with this without confirmation of this “ridiculous” query string which you’ve stated is the ONLY thing that needs to be matched to be redirected.

Then, does the generated link include the nl subdirectory? If not, then the code is working as intended (as specified, anyway).

Specificity, if you please!

Regards,

DK

RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65$
RewriteRule ^index\\.php$ index.php [R=301]

That works fine so the string must be fine. However, this:

RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65$
RewriteRule ^nl/index\\.php$ index.php [R=301]

does not.

This will work fine in transferring from root to the NL domain on matching that string:

RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65$
RewriteRule ^index\\.php$ nl/index.php [R=301]

and so I would expect the reverse to work:

RewriteCond %{QUERY_STRING} ^page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=65$
RewriteRule ^nl/index\\.php$ index.php [R=301]

However, it doesn’t. The nl/ part of the URL is also generated as no NL directory exists. It’s a part of a translation extension and creates subfolders for different languages nl/ ja/ de/ etc. Thanks, Ant.

I still can’t figure out why nl/index.php won’t work. I guess it must be because the nl/ part of the URL is dynamically generated as well. Do you know how I could specify that the URL has two dynamic parts? Thanks for your time. Ant.

Ant,

Would you PLEASE address the duplicated Itemid=65 in your query string? Is this whole thing generated? Generated as “page=checkout.index&option=com_virtuemart&Itemid=65&redirected=1&Itemid=6 5”? I feel that we’re not going anywhere with this without confirmation of this “ridiculous” query string which you’ve stated is the ONLY thing that needs to be matched to be redirected.

Then, does the generated link include the nl subdirectory? If not, then the code is working as intended (as specified, anyway).

Specificity, if you please!

Regards,

DK

I don’t understand the specificity you’re after. I want to turn one URL into another. Please specify the specifics you want. Thanks for your time. Ant.