Forum Moderators: phranque

Message Too Old, No Replies

Redirect only a few dynamic URLs

         

jancek

1:19 pm on Sep 2, 2011 (gmt 0)

10+ Year Member



Hi,

I am wondering if there is a way to redirect a single dynamic URL to another dynamic URL using htaccess.
for example domain.com/sth.php?id=X to domain.com/sthelse.php?id=Y
I need to redirect maybe 10 URLs like this and there is not any logical relation between Xs and Ys. That is why I cant create rule for this (maybe it's even not possible).

lucy24

1:45 am on Sep 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If there's no visible connection between the different items, you may really have to make ten separate rules with ten separate conditions. The Conditions are needed because RewriteRule by itself doesn't see query strings (parameters)-- the part after the question mark. Here is the boilerplate. Ignore the parts that don't apply to you.
Query Strings

The Query String, also known as a Parameter, is the part of an url after the question mark. Question = query.

By default, rewrites simply ignore the query string. That is, mod_rewrite stashes the query in a safe place, does its stuff to the part before the question mark, and then reappends the original query.

Changing a Query

#1 To delete a query, add a ? to the end of your rewrite target.
#2 To replace a query—or create a new one—add ?blahblah to the rewrite target. The blahblah can be either literal text, or stuff you captured earlier. (#1 and #2 are really the same thing: you're just replacing the query with either something or nothing.)
#3 To add to an existing query, again put ?blahblah at the end of the target, but also add [QSA] to your flags (the bracketed items at the end of the Rule). It stands for "Query String Append", meaning that the blahblah is to be added to the existing query—if any—instead of replacing it.

Getting the Query

You only need to retrieve the original query if
#1 you want the rewrite to behave differently depending on what the query was
or
#2 you need to change or delete the query

Add a Condition that says

RewriteCond %{QUERY_STRING} blahblah


using your ordinary Regular Expressions, anchors and ! as needed.

To test whether there was a query at all

RewriteCond %{QUERY_STRING} .


which simply means "If the query contains at least one character of any kind".

If you need to capture any of the query, use parentheses as usual. In the rewrite target, the captures will be %1, %2 etc instead of $1, $2 etc, because they are coming from a Condition instead of the Rule. Each set is separately numbered, so the first capture from the Rule will still be $1.

jancek

7:36 am on Sep 5, 2011 (gmt 0)

10+ Year Member



Thank you lucy42!
It is partly working.
This rule is redirecting correctly.
RewriteCond %{QUERY_STRING} 003
RewriteRule ^url1\.php$ [domain.com...] [R=301,L]

I need to do the same for english version of the website (same htaccess file). The following rule is not working, not redirecting at all.

RewriteCond %{QUERY_STRING} 003
RewriteRule ^en/url1\.php$ [domain.com...] [R=301,L]

Is this because of the query string is the same for both conditions?

g1smd

7:55 am on Sep 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use
example.com
in the forum to make the post readable.

The code should work. The RewriteRule RegEx pattern is evaluated before the RewriteCond pattern.

Use Live HTTP Headers to see the server responses. Clear the browser cache before each test.

jancek

8:02 am on Sep 5, 2011 (gmt 0)

10+ Year Member



This rule is redirecting correctly.
RewriteCond %{QUERY_STRING} 003
RewriteRule ^url1\.php$ http://www.example.com/url2?id=033 [R=301,L]

I need to do the same for english version of the website (same htaccess file). The following rule is not working, not redirecting at all.

RewriteCond %{QUERY_STRING} 003
RewriteRule ^en/url1\.php$ http://www.example.com/en/url2?id=033 [R=301,L]

lucy24

8:41 am on Sep 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For starters: are your rules anchored exactly as you've shown them here? Sometimes it doesn't matter, but here it is crucial, because you want to pick up

www.example.com/url1.php

but not

www.example.com/en/url1.php

Come to think of it... are "url1" and "url2" really the same in the English and non-English versions, or did you just use those for your examples? If they are the same, you can simply say

RewriteCond %{QUERY_STRING} 003
RewriteRule ^(en/)?url1\.php$ http://www.example.com/$1url2?id=033 [R=301,L]


Also try a direct copy-and-paste from your htaccess into another document. Make sure you haven't got something nefarious like a non-breaking space sneaking in where it isn't wanted. This would seem wildly improbable if there hadn't been so many posts reporting exactly that problem. Though not, admittedly, in .htaccess files.

And, conversely, make sure there is exactly one space between each piece of the Rules and Conditions. htaccess can be very very very persnickety. Look at your site logs and see if they have anything to say. Error logs are not likely to be useful unless you get a full-blown 500. Then they can sometimes point you in exactly the right direction.

jancek

9:09 am on Sep 5, 2011 (gmt 0)

10+ Year Member



The rules are anchored exactly as I have written.
URL1 and URL2 are the same in english and non-english version. I changed the rule to:
RewriteCond %{QUERY_STRING} 003
RewriteRule ^(en/)?url1\.php$ http://www.example.com/$1url2?id=033 [R=301,L]

but no success with 'en' version. Non-english version redirection is working also after changing the rule.

g1smd

9:14 am on Sep 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Some other rule must be interfering with the correct operation of this rule.

jancek

8:02 am on Sep 7, 2011 (gmt 0)

10+ Year Member



How can the interfering rule look like? I have no rule with same condition or rule starting with en/url1

g1smd

8:04 am on Sep 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's many things it could be.

Any previous rule that begins .* would take priority for example.

lucy24

7:07 pm on Sep 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does the "en" version redirect to something else, or does nothing happen to it at all? You'll have to fine-tooth-comb the earlier part of your htaccess and look for anything that could pick up /en/ (even though you didn't intend it to) and make it more specific, so it only picks up the parts it was meant to pick up.

Necessary disclaimer: "earlier part" doesn't necessarily mean physically higher up on the htaccess page. Apache runs in "sweeps", with each module getting completely processed before it moves on to the next one. If your htaccess is a tangled mess, this is the time to clean it up.

:: uneasily trying to figure out how <Files> can override [F] if the Core runs last of all ::

jancek

8:08 am on Sep 8, 2011 (gmt 0)

10+ Year Member



en version is not redirected at all.

lucy24

7:26 pm on Sep 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As g1 said above, there has to be some earlier rule that is rewriting /en so it isn't recognized when it hits your main redirect rule. Come to think of it, is is getting rewritten? That is, when you enter the /en form of the url, does the browser's address bar correspond to both what you typed, and the actual contact of the page you're taken to?

Firefox Live Headers can be useful. Remember to turn it on before you start experimenting, so your results aren't contaminated by newly cached pages. Empty your cache at the beginning, and reload each page every time you go there. If you load up a page, change the htaccess and reload, any changes should come through.

jancek

7:49 am on Sep 9, 2011 (gmt 0)

10+ Year Member



No. The EN version is not rewritten. I am using HTTPFox in Firefox.