Welcome to WebmasterWorld Guest from 54.163.168.15

Forum Moderators: Ocean10000 & incrediBILL & phranque

htaccess redirect page with parameters to page with no extension

   
11:25 pm on Feb 8, 2013 (gmt 0)

5+ Year Member



I have a page with this url
http://example.com/members/index.php?option=com_content&task=view&id=163&Itemid=193

I want to redirect them permanently to this kind of url
[newsite.com...]

when I do anything, it seems to ignore this... I've tried...

RewriteRule (http://example.com/members/index.php?option=com_content&task=view&id=163&Itemid=193)$ [newsite.com...] [R=301,L]

and

Redirect 301 /members/index\.php?option=com_content&task=view&id=163&Itemid=193 [newsite.com...]

and

RewriteRule ^/members/index\.php?option=com_content&task=view&id=163&Itemid=193$ [newsite.com...]

what happens is nothing, or it redirects to the [newsite.com?option=com_content&task=view&id=163&Itemid=193...]

any clues? I can't seem to find my exact problem. I want this specific page with parameters to redirect to the specific page with no extension without sending the parameters.
11:59 pm on Feb 8, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



You need a preceding RewriteCond looking at the query string parameters.

The RewriteRule pattern looks only at the path part of the URL request. It cannot match parameters.

Do not use Redirect or RedirectMatch.

Use example.com in this forum to suppress URL auto-linking.
12:17 am on Feb 9, 2013 (gmt 0)

5+ Year Member



OK, this is how it ended up looking when I got done.. two conditions may be overkill, but it only worked this way for me...

RewriteCond %{QUERY_STRING} ^(.*&)?id=163(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?Itemid=193(&.*)?$ [NC]
RewriteRule ^members/index\.php$ http://example.com/page_name? [L,R=301]

[edited by: incrediBILL at 5:18 am (utc) on Feb 9, 2013]
[edit reason] disabled smilies [/edit]

1:14 am on Feb 9, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



All of the .* bits are redundant, as are the "(" and ")" and "?" and [NC] each time.

Use simpler anchoring like
(^|&)
and
(&|$)
around each parameter instead.

"Disable graphic smile faces for this post?" is your friend.
4:40 am on Feb 9, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Aside from all that...

Just how many pages are affected? 163 ids x 193 Itemids is a heck of a big number. Or is this is the only query string that actually occurs?

The pattern
^(.*)
with leading wild-card is never appropriate. The pattern
(.*)$
with closing anchor is never necessary. The anchor provides no additional information and sets no new constraints.

The forms (^|&) and ($|&) mean "the current parameter begins or ends at this point".* Yes, the pipes are separating non-parallel things: an anchor on one side, a literal character on the other. That's why you can't say simply [^&] or [$&].

Never use [NC] in a rewrite. (Except hotlinking routines, which are analogous to [F] so they don't count.) In a redirect, use it only if multiple casings really do occur. F'rinstance: "\.jpg [NC]" doesn't mean the same as "\.(jpg|JPG)". It can also mean .Jpg or .JpG or .jPg et cetera, so the server keeps having to test against possibilities that will never really occur.

And why does the software think a close-parenthesis after an ampersand is by itself a wink?


* You can't tell from the timestamp, but I just detoured for about an hour** looking for the post where I serendipitously thought of some good examples of anchored and un-anchored queries. Couldn't find it :( It was something involving lan= something and plan= something else. Um.

** I did some other stuff too, like change water in the marine aquarium and wash dishes and give the animals their evening treats, except the hamster whose sleep schedule is strinkingly similar to mine.
4:00 pm on Feb 11, 2013 (gmt 0)

5+ Year Member



yeah, I'm not trying to make a bunch of pages forward... I just want this one page to forward... so.. what would be the proper sentence to use? Like this?

RewriteCond %{QUERY_STRING} (*|&)?id=163(&|$)?
RewriteCond %{QUERY_STRING} (*|&)?Itemid=193(&|$)?
RewriteRule ^members/index\.php$ http://example.com/page_name? [L,R=301]
6:12 pm on Feb 11, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



You'll need to re-edit that and tick the "disable smilies for this post" box.

The * should be ^ and the question marks in the Conditions are not needed.
8:16 pm on Feb 11, 2013 (gmt 0)

5+ Year Member



RewriteCond %{QUERY_STRING} (^|&)id=163(&|$)
RewriteCond %{QUERY_STRING} (^|&)Itemid=193(&|$)
RewriteRule ^members/index\.php$ http://example.com/page_name? [L,R=301]


better? lol... I'm assuming this is just looking for those particular strings, right?
8:24 pm on Feb 11, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Yes. Both of those parameters must be present and can be in any order. It allows other unspecified parameters to also be present, and the request will still be redirected.

The
(^|&)
allows the following parameter to be at the beginning or not at the beginning of the query string. It also stops false matching for requests such as
blahid=163
or
fooItemid=193
etc.

The
(&|$)
allows the preceding parameter to either be not at the end or at the end of the query string. It also stops false matching for requests such as
id=163000
and
Itemid=193000
etc.
8:33 pm on Feb 11, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



the question marks in the Conditions are not needed

Worse than that: They're actively harmful, because they make the (&|$) element optional-- which in turn is equivalent to not having it there at all. The anchors may not be as crucial at the end as at the beginning, depending on whether you need to exclude forms like "id=1634" or "id=163abc", but don't take chances.
8:42 pm on Feb 11, 2013 (gmt 0)

5+ Year Member



lol, thank you both for the education. so, I was saying "ok, if it's this page or maybe even not this page, then redirect if you feel like it" sounds like what my hubby thinks I'm sayin when I wanna move furniture...

htaccess continues to amaze me with it's power...
8:56 pm on Feb 11, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



It is an incredibly powerful tool with a very concise and precise syntax.

It always does exactly what the code says it should do, rather than what you hoped or expected the code to do. :)
9:26 am on Feb 13, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



As if by magic, just one day later there's a thread where the entire problem rested on correct use of anchoring around parameters being tested: [webmasterworld.com...]
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month