Forum Moderators: phranque

Message Too Old, No Replies

.htaccess 301 - Redirects fine but won't drop trailing variables

         

polishhill

7:51 pm on Apr 16, 2010 (gmt 0)

10+ Year Member



I have taken over a site that needs some redirects. I have added code to the .htaccess that redirects some dead directories. Those dead directories had many pages, which were linked to via url variables.

The problem is that my redirect works, but doesn't eliminate the trailing variables: Example

domain.com/dir/tut.mv?wakawaka needs to redirect to domain.com, but is currently redirecting to domain.com?wakawaka

Here is my redirect at its simplest iteration. I have tried many different things, but can't seem to get it to work Any help would be much appreciated!


RedirectMatch 301 /dir/tut.mv
[size=2]http://www.domain.com[/size]

g1smd

8:42 pm on Apr 16, 2010 (gmt 0)

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



I'd use a
RewriteRule
with
[R=301,L]
flags and add a trailing slash plus a question mark at the end of the target URL to clear any appended query string data.

Add a preceding
RewriteCond
that detects that a query string, any query string, is present. This step is vital to prevent an infinite loop.

[edited by: jdMorgan at 2:16 am (utc) on Apr 17, 2010]
[edit reason] Tidied typo [/edit]

cjezowicz

7:12 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



I didn't understand this when I first read it but it did help me find the answer.

If you want to redirect ALL variables on a certain page in this case the homepage use this in your .htaccess

#Redirect All Variables
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.*)$ http:// www.yourdomain.com/$1? [R=301,L]

RewriteCond %{THE_REQUEST} \?\ HTTP [NC]
RewriteRule .? http:// www.yourdomain.com%{REQUEST_URI}? [R=301,L]

(be sure to replace yourdomain)

Follow Up Question

Do you know how to redirect a single unique query string to a different page.
Ex. mydomain.com/?a=forums needs to goto mydomain.com/forums/ instead of redirecting to the homepage since it is also a query string.

jdMorgan

7:50 pm on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Redirect from URL with query string to URL with different query string: [webmasterworld.com...]

Jim

cjezowicz

8:46 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



Just wanted to post what I ended up with

#RedirectMatch 301 something to /Forums/
RewriteCond %{QUERY_STRING} ^a=forums(.*)$
RewriteRule ^(.*)$ http:// www.mydomain.com/forums/$1? [R=301,L]

In this case the variable tacked onto the homepage is ?a=forums and I want it to goto /forums/. I added the (.*) after "forums" because there are subdirectories that would also benefit from being redirected.

If you see any potential problems with any of this please let me know.

g1smd

10:33 pm on Apr 19, 2010 (gmt 0)

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



The (.*) in the RewriteCond is redundant as you don't use %1 anywhere else.

The $ symbol is also not required.

Update the Comment to not mention 'RedirectMatch' as you are using RewriteRule now.

cjezowicz

2:02 pm on Apr 20, 2010 (gmt 0)

10+ Year Member



The (.*) in the RewriteCond is redundant as you don't use %1 anywhere else.
Is it redundant because I can get rid of $ which if I'm not mistaken declares the end of the search string?

Update the Comment to not mention 'RedirectMatch' as you are using RewriteRule now.
Hah, thanks. Simple overlook, I just copy and paste other rules in my .htaccess butchering them together :P

Thanks guys, I appreciate all the help!

g1smd

6:13 pm on Apr 20, 2010 (gmt 0)

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



This pattern
^a=forums
will match anything beginning with
a=forums
.

What follows might not be important. It will match
a=forumstuffrandomword
and
a=forums&param=value
etc too.

polishhill

4:47 pm on Apr 23, 2010 (gmt 0)

10+ Year Member



Ok, so if it is not clear, cjez and I are working on this as a team. We have created the redirects and they seem to be fine but google webmaster tools is still reporting them as not found. Did we do something wrong, or will it just take google some time to adjust? Here are our header responses:

#1 Server Response:
http://www.domain.com/xx/store/display.mv

HTTP Status Code: HTTP/1.1 301 Moved Permanently
Date: Fri, 23 Apr 2010 16:37:16 GMT
Server: Apache
Location:
http://www.domain.com

Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
Redirect Target:
http://www.domain.com


#2 Server Response:
http://www.domain.com

HTTP Status Code: HTTP/1.1 200 OK
Date: Fri, 23 Apr 2010 16:37:16 GMT
Server: Apache
Transfer-Encoding: chunked
Content-Type: text/html

Thank you so much for your help!

jdMorgan

7:24 pm on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see a query string of "a=forums<xyz>" in either header report.

So I don't think this is a valid test.

Jim

cjezowicz

8:25 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



It is a valid report, it's just not related to the "a=forums<xyz>" redirect we were dealing with before. Basically we determined the cause for some of our double redirects. We were just trying to figure out if there are any negative side effects in the situation he just described.

Thanks,

Chris

jdMorgan

3:04 am on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "Location" header indicates a redirect to "www.example.com". This is problematic, because that's not really a valid URL.

The redirect target should be "www.example.com/" (with a trailing slash) to make it a URL instead of just a "protocol-plus-hostname string."

Jim

cjezowicz

5:57 pm on Apr 29, 2010 (gmt 0)

10+ Year Member



Thanks jdMorgan, I wouldn't have gotten that without you! Now we just have to wait for Google to recognize the changes; all the redirects work properly.

Thanks for all your help so far! I'm sure we'll have more questions as we expand our site. :D