Forum Moderators: Robert Charlton & goodroi

Message Too Old, No Replies

Should 301 Redirects Have a "?" at the end?

         

Planet13

7:54 pm on Apr 12, 2011 (gmt 0)

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



Hi there, everyone:

I have been told by my hosting company that when doing 301 redirects, the destination URL should have a ? at the end of it. Is that a good idea? Or will it cause problems because both the URL with and without the ending question mark have a 200 server response.

For example, in my .htaccess file, the redirects are written as:


Redirect 301 /widgets/old_page.html http://www.newdomain.com/new_page.html?


I think they told me the reason to do this is so that the original request doesn't get appended to the end of the destination URL. Otherwise, if I don't put the ending ? on the destination URL, I thought it would redirect to:

http://www.newdomain.com/new_page.html/widgets/old_page.html


any input on whether having the ? at the end of the URL is a bad thing or not is appreciated.

g1smd

8:57 pm on Apr 12, 2011 (gmt 0)

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



Half right.

The question mark on the end of a RewriteRule target URL stops any parameters being reappended. It doesn't actually add a question mark to the visible URL.

This stuff is easy enough to test. Add a rule involving URLs that don't actually exist on the server (for example "xyz123" and "abc789"), then request one and see what the URL in the browser URL changes to.

Planet13

9:41 pm on Apr 12, 2011 (gmt 0)

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



@g1smd:

Thanks for the response.

It doesn't actually add a question mark to the visible URL.


I apologize in advance if I don't understand you correctly, but...

In my case, it DOES add a question mark to the end of the visible URL (of the page it is directed to).

So when it loads in the browser, the URL bar displays:

http://www.newdomain.com/new_page.html?


This displays the same page as without the URL. Both return a 200 response.

One thing to note:

In the .htaccess file, the redirect 301 all need the ? at the end, or the first argument gets appended to the end of the destination URL.

However, if I do a REWRITE instead of a redirect, then there is no need to add the question mark to the destination URL in the htaccess file.

I am beginning to this that I am going to have to change all the redirects to rewrites instead (if it is important not to have the ? at the end of the URL).

g1smd

10:13 pm on Apr 12, 2011 (gmt 0)

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



You need an external redirect here, not an internal rewrite.

Use
RewriteRule
syntax for those external 301 redirects and use the final question mark to stop query strings being reappended.

RewriteRule ^old-path$ http://www.example.com/new-path? [R=301,L]


I never use
Redirect
syntax as it is very limiting in what it can do.

Planet13

10:43 pm on Apr 12, 2011 (gmt 0)

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



You need an external redirect here, not an internal rewrite...

RewriteRule ^old-path$ http://www.example.com/new-path? [R=301,L]


Thank you, g1smd, for the tip and for the syntax. I will try and get those taken care of.

I hope that I have not shot my self in the foot though, since google has "seen" those URLs with the ? at the end (although the canoncial link tag has the URL WITHOUT the ? and google has indexed my pages without the ? at the end).

Thanks again.

g1smd

10:48 pm on Apr 12, 2011 (gmt 0)

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



Those can be redirected too, but the rule is somewhat more complex. The question mark is not part of the path nor the query string, it is merely a separator between those two parts.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^\?]*\?\ HTTP/
RewriteRule (.*) http://www.example.com/$1? [R=301,L]


Untested. May need tweaking. Post if it doesn't work.


So the full job has three parts. First fix the rules for the old to new URL redirects so that no question mark is added. Next, add a rule to fix "with question mark" to redirect to "without question mark". Finally, make sure you have your general non-www to www canonicalisation rules in place. These three things will stop the generation of new errors and will also fix errors already found out in the wild.