Forum Moderators: phranque

Message Too Old, No Replies

Query string redirect

all pages

         

steveb

11:59 pm on Dec 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have this in htaccess

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

This works fine to redirect query strings on my main page. Is there a way to write it so query strings on all pages redirect?

Ideally I'd like to 301
www.example.com/dir1/?ahahha
to
www.example.com/dir1/

but if I could redirect all query strings simply to the index page, that would be okay too. (I don't use query strings for anything, so I don't really care what page people would end up at.)

Thanks.

jdMorgan

3:43 am on Dec 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you just want to remove the query string and keep the page, then this should do:

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

Jim

steveb

6:38 am on Dec 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Works great. Thanks.

rjwmnews

5:49 pm on Dec 30, 2006 (gmt 0)



jdMorgan -- the first of the following two rewrite examples for removal of a query string, I believe but am not certain, was also provided by you. Please advise if any fundamental difference exists in execution between the two and/or if one solution is preferred for any reason such as efficiency. Does the second example only consider URIs with a query string component? Thanks.

RewriteRule ^(.*)$ http://www.example.com/$1? [R=301,L]

-- OR --

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

jdMorgan

9:53 pm on Dec 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Does the second example only consider URIs with a query string component? Thanks.

Yes, and the first example, standing alone without a RewriteCond as shown, would create an 'infinite' redirection loop.

Jim

steveb

2:28 am on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, I'm an idiot. I do have a few uses for tracking links that uses query strings, so this code doesn't allow me to use them.

Question 1: Is there a way to use the code above, but allow query strings for a specific directory or even a specific URL?

Question 2: I tried this...

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

but it didn't make
http://www.example.com/dir1/?blah
redirect to
http://www.example.com/dir1/

Is there a way to do this for a single internal page? Or (better) all pages within one directory?

jdMorgan

2:52 am on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




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

Jim

steveb

7:33 am on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, that didn't work for me. The query string didn't redirect.

jdMorgan

8:11 am on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As shown, all pages in "dir1/" with query strings will be redirected to remove the query string.

If that's not what you wanted, then please be specific (no multiple choices)... The code has to be explicit.

Also, the intent of this forum is to encourage you to ask questions, experiment, and learn. The resources cited in our forum charter and the threads in the Apache section of the WebmasterWorld library may prove helpful.

Jim

steveb

9:33 am on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry if I wasn't clear. No pages in dir1/ redirected. Basically nothing redirected after I added the code. I'd like everything in that directory to redirect. I don't know what I did wrong, but adding that code stopped everything from redirecting.

I only had
RewriteCond $1 ^dir1/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

Should I have had...
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
RewriteCond $1 ^dir1/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

jdMorgan

5:07 pm on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the code located in .htaccess in dir1/ or in a directory level above it?

Using the snippet I posted, as written, all URLs pointing to example.com/dir1/ with query strings should be redirected to remove the query string. So it may be that the code is in the wrong location.

I'm also assuming you've flushed your browser cache after each change to the code, and that no other rewriterule preceding this one --in the same .htaccess file or in a higher-level .htaccess file-- is interfering by rewriting these URLs before the new code can be executed.

Jim

steveb

11:32 pm on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"Is the code located in .htaccess in dir1/ or in a directory level above it?"

The level above it. I have no htaccess file in /dir1/

Should I put an htaccess file in /dir1/ and add only those three lines above to it?

"...and that no other rewriterule preceding this one --in the same .htaccess file or in a higher-level .htaccess file-- is interfering by rewriting these URLs before the new code can be executed...."

I got a bunch, this is what I have now in the root level:

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^123.45.678.90 [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} .
RewriteRule ^$ http://www.example.com/? [R=301,L]

All these are basically more important than this new thing I want to do. But can I add a new htaccess to /dir1/ that only says...

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

This would be a great solution for me since I could just pick specific directories to add it too, but leave everything else alone.

jdMorgan

1:10 am on Jan 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well if you inserted the new rule after your existing ones, and did not change the semantics of "^dir1/" when replacing it with your real directory name, I see no reason why it should not have worked, given that you flushed your cache before testing it. This isn't exactly a complex rule, but I see nothing wrong with it, or any source of interference in the other rules your posted.

Should you choose to try it in the dir1/ subdirectory, the code needs to change to reflect its new location:


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

[edit] speling [/edit]

Jim

[edited by: jdMorgan at 1:10 am (utc) on Jan. 1, 2007]

steveb

6:05 am on Jan 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay I see my screwup. I replaced

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

with

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

instead of adding it beneath it.

So now if I wanted to do this with multiple directories, I could add that other code in an htaccess file in each sub directory, or in the main htaccess can I do...

RewriteCond $1 ^dir1/
RewriteCond $1 ^dir2/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

Or would I need to do something else?

jdMorgan

8:08 am on Jan 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a couple of ways to do it. Either use explicitly-ORed RewriteConds like this:

RewriteCond $1 ^dir1/ [OR]
RewriteCond $1 ^dir2/ [OR]
RewriteCond $1 ^dir3/ [OR]
RewriteCond $1 ^dir4/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

or use an in-line OR function, like this:

RewriteCond $1 ^(dir1¦dir2¦dir3¦dir4)/
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

The latter is more efficient in .htaccess, while the former is more efficient in server config files, and is somewhat easier to maintain because the conditions stand out better visually.

Note in the first example that there is no [OR] on the last "dir" condition, and this is important.

Replace the broken pipe "¦" characters in the second example with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

steveb

8:16 am on Jan 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great. Thanks as always.