Forum Moderators: phranque

Message Too Old, No Replies

.htaccess how to cut away the parameter string

I want to redirect to a URL without parameter string

         

jetteroheller

8:02 am on Jan 9, 2007 (gmt 0)

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



From 1998 to 2000, I used a banner rotation system with a parameter string in the URL

http://www.example.com/target/page.htm?V=3&from=widget

Today, there are still visits of Googlebot with such parameter strings.

So I want something in my .htaccess to cut away all behind "?" or "&" or "#" and to redirect to the URL without the parameter string.

phranque

8:49 am on Jan 9, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you could try this:

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

jdMorgan

2:16 pm on Jan 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If only one page, or if only ".htm" pages have the query string attached, I'd suggest making the RewriteRule more specific, as this will eliminate unnecessary RewriteCond processing.

For example, to process .htm files only:


RewriteCond %{QUERY_STRING} .
RewriteRule ^([^.]+)[b]\.htm[/b]$ http://www.example.com/$1[b].htm[/b]? [R=301,L]

Or to process only the single page.htm file:


RewriteCond %{QUERY_STRING} .
RewriteRule ^[b]page\.htm[/b]$ http://www.example.com/$[b]page.htm[/b]? [R=301,L]

In fact, if any other pages on your site still use query strings, you will have to use a specific pattern to avoid stripping their query strings.

Jim

phranque

12:22 am on Jan 10, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



what he said!

i was assuming the most general case in my response.
avoid the ambiguous, greedy and promiscuous ".*" whenever possible.

jetteroheller

7:29 pm on Jan 19, 2007 (gmt 0)

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



In fact, if any other pages on your site still use query strings, you will have to use a specific pattern to avoid stripping their query strings.

Forgott, that I have Google AdSense search,
this creates in URLs like

/search.htm?domains=example.com&q=&sa=Google+Search

jetteroheller

1:25 pm on Jan 22, 2007 (gmt 0)

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



Now I have in my .htaccess

RewriteCond %{QUERY_STRING} ^B= [NC,OR]
RewriteCond %{QUERY_STRING} ^D= [NC,OR]
RewriteCond %{QUERY_STRING} ^N= [NC,OR]
RewriteCond %{QUERY_STRING} ^M= [NC,OR]
RewriteCond %{QUERY_STRING} ^S= [NC,OR]
RewriteCond %{QUERY_STRING} ^V= [NC,OR]
RewriteCond %{QUERY_STRING} ^i_mpg= [NC]
RewriteRule (.*) http://example.com/$1? [R=301,L]

But I can not figure out the Syntax of .htaccess

Instead of the list of forbidden string, I would like to allow only ond type of query string containing "domains=".

Something what would be in Perl

if ( index ( $QUERY_STRING ), "domains=" ) < 0 ) { cut away query sting and make 301 redirect }

jdMorgan

3:12 pm on Jan 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> query string containing "domains=".

# IF query string contains one or more characters
RewriteCond %{QUERY_STRING} .
# and IF query string does NOT contain "domains="
RewriteCond %{QUERY_STRING} !domains=
# THEN redirect to remove query string
RewriteRule (.*) http://example.com/$1? [R=301,L]

> But I can not figure out the Syntax of .htaccess

This is largely a discussion forum, so please review the code above character-by-character, checking against the mod_rewrite documentation cited in our forum charter [webmasterworld.com]. The tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com] may also be helpful.

Specific questions about the code are welcome.

Jim