Forum Moderators: phranque

Message Too Old, No Replies

Remove Query String Completely

         

brite

7:33 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Hello,

I've read some posts related to removing query strings, but I haven't gotten it to work properly. Hoping for some help...

I have this query string I need to remove: ?tmpl=component&print=1

I've tried a number of things, such as this:
RewriteCond %{query_string} ^(.+)tmpl=component&print=1 [NC]
rewriteRule ^portfolio-gallery/item/$ http://www.example.com/portfolio-gallery/item/$1 [R=301,L]

All I want is to remove the query all together
Sample:
http://www.example.com/my-url-here?tmpl=component&print=1
TO:
http://www.example.com/my-url-here

thanks for any help!

[edited by: jdMorgan at 4:22 pm (utc) on Jan. 8, 2010]
[edit reason] example.com [/edit]

g1smd

8:37 pm on Dec 22, 2009 (gmt 0)

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



End the target URL with a question mark and the query string will not be re-appended.

jdMorgan

9:26 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming you have other working rules -- that is, that the required mod_rewrite set-up directives are already present in your .htaccess file, here's a corrected and more-robust version:

RewriteCond %{QUERY_STRING} ^([^&]*&)*tmpl=component&print=1(&.*)?$ [NC]
RewriteRule ^portfolio-gallery/item/$ http://www.example.com/portfolio-gallery/item/$[b]1?[/b] [R=301,L]

The 'enhanced' query string pattern will match if additional name/value pairs are present in the query string *and* requires that the 'tmpl' name be exact. That is, it won't match a name of "newtmpl" since it requires that if any character precedes "tmpl", then it must be an ampersand. The same is true at the end: a value of "1" for "print" will match, but a value of "10" now won't match.

If the two name/value pairs might ever be reversed (in your links or inlinks from other sites), then you should account for that possibility, too -- Just break that RewriteCond into two ReewriteConds, each testing only one of the name/value pairs, and with the same leading and trailing subpatterns as shown above.

Jim

brite

3:12 pm on Jan 8, 2010 (gmt 0)

10+ Year Member



Thank you very much for the replies.

I have yet to get this to work. I have other rewrite rules that work, yet none that deal with query strings. The directives preceding the Rewrite Rules I have in .htaccess are the following:

RewriteEngine on
Options +FollowSymLinks

Have I missed something?

Thank you again for the detailed helpful reply.

jdMorgan

4:21 pm on Jan 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I just noticed an inconsistency, in that "$1" is back-referenced in the RewriteRule substitution, but it is not defined in the RewriteRule pattern.

The implication is that perhaps you have more than one URL you want to redirect -- that is, maybe you wish to redirect other "pages" or "files" located in the "/portfolio-gallery/item/" subdirectory, or perhaps that trailing slash isn't always/ever present on the requested URL... something's inconsistent here, and we need to know what it is.

If more than the single *exact* URL "example.com/portfolio-gallery/item/" is to be redirected, then please post *several* examples of the requested URL-paths to be redirected by this rule, and make them all as different from each other as possible -- as long as all your examples are valid URLs that will resolve on your site. (Note: Please use only "example.com" as the domain.)

Note that the URL-path begins after the domain and ends just before the "?", and the query string starts just after the "?". The "?" itself is part of neither.

The regular-expressions patten in the rule must match the requested URL-path *exactly*, as must the RewriteCond query-string pattern, or no redirect will occur.

Also, what is the URL-path to this .htaccess file? Is it example.com/.htaccess, or is this .htaccess file located in a subdirectory below that point?

Jim

brite

2:56 pm on Jan 9, 2010 (gmt 0)

10+ Year Member



Good Morning Jim,

Thank you again for your time and help. I believe your rule is probably perfect. I have been testing several things and have realized that I am attempting to rewrite (redirect) a URL that itself has already been rewritten to a more SEF friendly version. I have another rewrite rule for a similar URL that causes no issues, however, I thought I would bring it up since I have no idea of the impact this may have with a Query String involved.

>> please post *several* examples of the requested URL-paths to be redirected by this rule, and make them all as different from each other as possible -- as long as all your examples are valid URLs that will resolve on your site.

1) http://www.example.com/portfolio-gallery/item/1-my-first-page?tmpl=component&print=1
2) http://www.example.com/portfolio-gallery/item/28-another-page?tmpl=component&print=1
2) http://www.example.com/portfolio-gallery/item/45-my-fortyfifth-entry?tmpl=component&print=1

>> Also, what is the URL-path to this .htaccess file? Is it example.com/.htaccess, or is this
—It is located in http://example.com/.htaccess

Many thanks. Sorry for not mentioning the SEF rules before...big amateur at all this.

jdMorgan

4:13 pm on Jan 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, it will be necessary to capture and back-reference the "page-number and title" bit at the end:

RewriteCond %{QUERY_STRING} ^([^&]*&)*tmpl=component&print=1(&.*)?$ [NC]
RewriteRule ^portfolio-gallery/item/(.+)$ http://www.example.com/portfolio-gallery/item/$1? [R=301,L]

The URLs to be rewritten by this likely *have not* "already been rewritten to a more SEF friendly version". They are likely new URLs linked on your pages as replacements for older non-search-friendly URLs." So these URLs are not the result of any rewriting, they are the result of your script (or a plugin) replacing the old "dynamic" links on your pages with SEF links.

I'm being picky here, because things can get very difficult if you don't realize/keep track of "precisely who is doing precisely what" to the URLs on your pages and the filepath references within your server. That's why we get tons of requests here monthly presuming that mod_rewrite can magically "change" all of the links published on a site's pages, when in fact it can't. All it can do (in this regard) is to 'map' the new URLs in those links back to the original script, so that the site still works, using the new SEF URLs to 'reach' the original script filepath...

Jim