Forum Moderators: phranque
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]
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]
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
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.
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
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.
RewriteCond %{QUERY_STRING} ^([^&]*&)*tmpl=component&print=1(&.*)?$ [NC]
RewriteRule ^portfolio-gallery/item/(.+)$ http://www.example.com/portfolio-gallery/item/$1? [R=301,L]
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