Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect dynamic to dynamic url's

         

darrenb

3:27 pm on Oct 11, 2009 (gmt 0)

10+ Year Member



I need to redirect old download manager url's to the new one

old
index.php?automodule=downloads
index.php?autocom=downloads

new
index.php?app=downloads

there are lots of dynamic url's though like

index.php?automodule=downloads&showcat=4
index.php?automodule=downloads&showfile=84

any ideas to try welcome and much appreciated.

jdMorgan

3:46 pm on Oct 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use mod_rewrite. Use a RewriteCond to check %{QUERY_STRING} and detect query names "automodule" and "autocom" and replace them with "app" in the RewriteRule, capturing and passing any other parameters through unchanged.

If "automodule=" and "autocom=" are always the first query parameters, then this is easy. If not, you'll need at least two RewriteConds to detect three cases: "automodule=" and "autocom=" at the beginning, "automodule=" and "autocom=" in the middle, and "automodule=" and "autocom=" at the end of the query string (If these query names are missing altogether, then none of these conditions will be met and the rule won't be invoked, so you don't need an explicit test for that case.) In practical terms, this can usually be accomplished with two RewriteConds.

Try searching WebmasterWorld (see "Search" link a top of this page) for "RewriteCond %{QUERY_STRING} RewriteRule" to turn up previous threads on this subject, take a look at the resources cited in our Apache Forum Charter and Library, and then post your best-effort at a coded solution as a basis for discussion.

Thanks,
Jim

darrenb

4:00 pm on Oct 11, 2009 (gmt 0)

10+ Year Member



Hi Jim,

"automodule=" and "autocom=" are always the first query parameters yes.

I have done much searching and reading but I'm a complete beginer and it's all just making my head spin.

Will search for the term you suggest and see how I get on. wish me luck.

Thanks,
darren

darrenb

4:20 pm on Oct 11, 2009 (gmt 0)

10+ Year Member



Ok here's my effort that does not work

rewritecond %{query_string} board/index.php?autocom=downloads* + board/index.php?autocom=downloads*
rewriterule board/index.php?app=downloads* [R=301,L]

Please don't laugh :(

darrenb

4:41 pm on Oct 11, 2009 (gmt 0)

10+ Year Member



Stopped it giving a 501 but the rewrite does'nt work:

RewriteEngine on
rewritecond %{query_string} ^board/index.php?automodule=downloads$ [OR]
rewritecond %{query_string} ^board/index.php?autocom=downloads$ [OR]
rewriterule board/index.php?app=downloads [R=301,L]

g1smd

6:02 pm on Oct 11, 2009 (gmt 0)

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



With [R=301] it is NOT a rewrite. It is a redirect.

With a redirect you should also specify the domain name in the target.

If you want a rewrite omit both the domain name and the [R=301] too.

QUERY_STRING can see ONLY the query string data, not any path part information.

RewriteRule needs both a pattern (to match the part-part of the requested URL) and a target. You have omitted one of those parts.

jdMorgan

6:13 pm on Oct 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No reason to laugh... The learning curve on mod_rewrite is initially quite steep, and if you're not conversant with regular-expressions pattern-matching, then that's a steep hill to climb as well.

You've got a bit of a mix-up of what's in the URL-path and what's in the query string appended to that URL-path, and there's no provision to 'keep' any other parts of the query string if they're present. Also, characters which have meaning as regular-expressions tokens must be escaped if you wish to match them as literal characters within a pattern (e.g a period is a regex token which matches any single character). Finally, it's a very good idea to specify a full canonical URL in the RewriteRule substitution to avoid problems under certain server configurations.

So this will likely work better:


RewriteCond %{QUERY_STRING} ^auto(module¦com)=downloads((&.+)¦&)?$
RewriteRule ^board/index\.php$ http://example.com/board/index.php?app=downloads%3 [R=301,L]

Important: Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Here "%3" in the substitution URL back-references (refers back to) to the matched contents of the third parenthesized sub-pattern in the RewriteCond, allowing any additional name-value pairs in the query string to be retained in the new URL. The rule will be invoked whether or not these additional parameters are present, because the entire second subpattern (and the third within it) is made entirely optional by the "?" following the parentheses.

Note also that I have replicated the "/board" URL-path-part that you put in your code although you didn't mention in your original post. So this code will work on requests for "example.com/board/index.php" assuming that this code is located in example.com/.htaccess.

Jim

darrenb

7:21 pm on Oct 11, 2009 (gmt 0)

10+ Year Member



Thanks jim,

That works better but if there is anything appended on the end of =downloads in the query string it does'nt work.

Thanks,
darren

jdMorgan

8:06 pm on Oct 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Make sure you changed both of the pipe characters as described above.

If that's not the problem, then more details are needed -- "It doesn't work" tells me almost nothing, so examples would be good as well as a description of what it should do and what it actually does.

Jim

darrenb

8:28 pm on Oct 11, 2009 (gmt 0)

10+ Year Member



Sorry Jim, missed the pipe in the second parenthesized sub-pattern. I stared at that bit for ages aswell trying to figure out how it worked.

So it works perfect now, I've got a way to go to get my head around this and my hat goes of to you Jim, you certainly know your stuff. Thank You for your help.

jdMorgan

9:11 pm on Oct 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> trying to figure out how it worked.

^auto(module¦com)=downloads((&.+)¦&)?$

If the query string starts with "auto", followed by "module" or "com", followed by "=downloads", followed by "&" and then anything else or just "&", then copy the "& and anything else" into %3 and invoke the rule. If the "ampersand and anything else" in %3 is blank, so be it. If there was just an ampersand after "=download", then drop it and don't copy it into the new query string. This latter feature is intended to prevent duplicate-content problems where you could end up with both "index.php?app=download" and "index.php?app=download&" URLs, due to either your own linking mistakes or the mistakes of others linking to you. Both of these URL variants would work, but likely return the same content, and that could potentially cause you "duplicate-content" trouble in search engine ranking. So all the 'fancy nested parenthetical constructs' are intended to copy the additional query parameters but drop empty ones.

You could also write it as:


RewriteCond %{QUERY_STRING} ^auto(module¦com)=downloads((&[^&]+)*)&?$
RewriteRule ^board/index\.php$ http://example.com/board/index.php?app=download[b]s%2[/b] [R=301,L]

and actually, I like that solution better than the one I posted above.

As before, replace the broken pipe character...

Jim

darrenb

1:41 am on Oct 26, 2009 (gmt 0)

10+ Year Member



Thanks for the explaination Jim, it's much clearer now. I agree your second solution is better.