homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

htaccess redirect page with parameters to page with no extension

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

I have a page with this url

I want to redirect them permanently to this kind of url

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]


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


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)

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)

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)

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)

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)

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)

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)

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)

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.

(^|&) 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.

(&|$) 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)

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)

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)

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)

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...]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved