Forum Moderators: phranque

Message Too Old, No Replies

301 redirect issue with exclamation and question marks

         

wedgin

4:22 am on May 7, 2009 (gmt 0)

10+ Year Member



I've just ported my site to wordpress from another CMS and discovered I had this url to 301 redirect

http://www.example.com/archive/2007/08/29/New_Vancouver_Canucks_Jerseys!?p=2007/08/29/New_Vancouver_Canucks_Jerseys_

which I want to resolve to

http://www.example.com/archive/2007/08/29/new_vancouver_canucks_jerseys/

Problem I'm running into is that I have both a ! and ? in the url and it refuses to redirect. If I remove those 2 characters from the url it resolves fine using a wildcard. Any help on this would be greatly appreciated. Thanks

[edited by: jdMorgan at 2:46 pm (utc) on May 7, 2009]
[edit reason] example.com [/edit]

g1smd

11:08 am on May 7, 2009 (gmt 0)

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



Use a
RewriteRule
and test the
Query_String
part with a preceding
RewriteCond
.

Escape the

!
like
\!
as a single
!
means 'not' on its own.

Make sure the

RewriteRule
forces both the www and the domain name and ends with
[R=301,L]
.

wedgin

2:27 pm on May 7, 2009 (gmt 0)

10+ Year Member



Here is what I tried and still no luck. I have a very rudimentary understanding of htaccess, so I have no idea what the proper way of writing this code would be. I've noticed I'm having a couple of URLs with ?p= not resolving now, so I need to deal with those too

RewriteCond %{QUERY_STRING} ^p=1$
RewriteRule /archive/2007/08/29/New_Vancouver_Canucks_Jerseys\!^$ http://www.example.com/archive/2007/08/29/new_vancouver_canucks_jerseys/ [R=301,L]

[edited by: jdMorgan at 2:46 pm (utc) on May 7, 2009]
[edit reason] example.com [/edit]

jdMorgan

3:00 pm on May 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



According to the query string example you gave in your initial post, the RewriteCond should be

RewriteCond %{QUERY_STRING} ^p=2007/08/29/New_Vancouver_Canucks_Jerseys_$

The problem may also be related to the exclamation point itself, and the encoding/un-endcoding of that character. To see if that is the problem, you could always try leaving the end-anchor off the rule pattern, to match any requested URL path that starts with "/archive/2007/08/29/New_Vancouver_Canucks_Jerseys" and ignore anything that follows:


RewriteRule ^/archive/2007/08/29/New_Vancouver_Canucks_Jerse[b]ys[/b] http://www.example.com/archive/2007/08/29/new_vancouver_canucks_jerseys/ [R=301,L]

Important: You did not state where you placed this code. If this code is put into an .htaccess file or into a <Directory> container in a server config file, then the requested URL-path 'seen' by RewriteRule will not start with a slash. In those cases, you will need to remove the leading slash from the RewriteRule pattern. (This could in fact be your main problem.)

Jim

wedgin

3:11 pm on May 7, 2009 (gmt 0)

10+ Year Member



My htaccess file is located in the root directory of example.com if that clarifies things. I'm going to give that a shot and see how it goes.

[edited by: jdMorgan at 3:40 pm (utc) on May 7, 2009]
[edit reason] Please use example.com only. See TOS. [/edit]

wedgin

5:05 pm on May 7, 2009 (gmt 0)

10+ Year Member



I tried this code and still no go, tried with and without the leading / too

RewriteCond %{QUERY_STRING} ^p=2007/08/29/New_Vancouver_Canucks_Jerseys_$
RewriteRule ^/archive/2007/08/29/New_Vancouver_Canucks_Jerseys http://www.example.com/archive/2007/08/29/new_vancouver_canucks_jerseys/ [R=301,L]

Any more ideas as to how I can tackle this?

jdMorgan

5:32 pm on May 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We're peering at your site through a drinking straw here...

Do you have any other working rules? -- Do you have the required "Options" and "RewriteEngine on" directives ahead of your RewriteRules?

Is this the exactly-correct and complete URL and query that you wish to redirect?
example.com/archive/2007/08/29/New_Vancouver_Canucks_Jerseys!?p=2007/08/29/New_Vancouver_Canucks_Jerseys_

Do you wish to remove the query string appended to the originally-requested URL? If so, add a question mark at the end of the new URL in the RewriteRule.

Jim

wedgin

5:40 pm on May 7, 2009 (gmt 0)

10+ Year Member



Here is what is above all my 301 redirects

# BEGIN WPSuperCache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
AddDefaultCharset UTF-8
RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_¦wordpress¦wp-postpass_).*$
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz -f
RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz [L]

RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_¦wordpress¦wp-postpass_).*$
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html -f
RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html [L]
</IfModule>

# END WPSuperCache

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

It is kinda the right URL, only my url has "what the fudge" shortform in it, which was getting replaced by the forum automatically when I pasted it it, so I took it out so as not to further muddy the issue.

I do wish to remove the query string at the end, it has no reason to be a query string. Obviously my old CMS let that page resolve, so it got indexed in google.

jdMorgan

6:35 pm on May 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rule order matters.

To avoid undesirable results, always put all of your external redirects before any of your your internal rewrites, in order from most-specific patterns --and conditions-- (fewest URLs affected) to least-specific pattern (more URLS affected), followed by your internal rewrites, again in order from most-specific pattern to least-specific pattern.

As you describe your rule-order now, your WP rewrite (your third ruleset) will be invoked for any and all requested URLs which do not resolve to a physically-existing file. As a result, your new redirect will never execute if it follows the WP rule.

Jim

jdMorgan

7:11 pm on May 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By the way, you can likely replace your first two rules with one, and save your server a whole lot of extra work:

RewriteCond %{REQUEST_URI} ![^/]$
RewriteCond %{REQUEST_URI} !//
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} ![=]
RewriteCond %{HTTP:Cookie} !(comment_author_¦wordpress¦wp-postpass_)
RewriteCond .gz>%{HTTP:Accept-Encoding} ^((\.gz)>.*gzip¦\.gz>)
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html%2 -f
RewriteRule ^(.*)$ /wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html%2 [L]

Replace all broken pipe "¦" characters with solid pipes before use: Posting on this forum modifies the pipe characters.

Here, requests from clients which do accept gzipped contents will result in %2 being defined as ".gz" and inserted into the substitution URL-path. If the request is from a client which does not accept gzipped content, then %2 will be blank, and nothing will be added to the end of the substitution URL-path.

This saves repeatedly processing all the preceding conditions, and prevents a redundant and wasteful call to the OS filesystem to do a second "file-exists" check. Additional minor tweaks for efficiency.

You can also get rid of the redundant lines


</IfModule>
#
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

between your first two rules and your third rule.

And strictly speaking, the <IfModule> conatiner isn't even needed, unless you plan to deploy this code on several servers, some of those servers do not support mod_rewrite, and you want the code to fail silently on those servers.

It's a good investment to make your configuration code as efficient as possible. Doing so can put off a forced upgrade to a better/more expensive server and/or hosting account as your traffic grows over time.

Jim

[edited by: jdMorgan at 7:14 pm (utc) on May 7, 2009]

wedgin

12:38 am on May 8, 2009 (gmt 0)

10+ Year Member



This is what I'm down to right now with my htaccess and it's still not working after rearranging my code and optimizing as per your last message. Thanks for taking a look at that, I had no idea it could be optimized like that.


RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^p=2007/08/29/New_Vancouver_Canucks_Jerseys_$
RewriteRule ^/archive/2007/08/29/New_Vancouver_Canucks_Jerseys http://www.example.com/archive/2007/08/29/new_vancouver_canucks_jerseys/ [R=301,L]

# BEGIN WPSuperCache
AddDefaultCharset UTF-8
RewriteCond %{REQUEST_URI} ![^/]$
RewriteCond %{REQUEST_URI} !//
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} ![=]
RewriteCond %{HTTP:Cookie} !(comment_author_¦wordpress¦wp-postpass_)
RewriteCond .gz>%{HTTP:Accept-Encoding} ^((\.gz)>.*gzip¦\.gz>)
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html%2 -f
RewriteRule ^(.*)$ /wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html%2 [L]

# END WPSuperCache

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

[edited by: jdMorgan at 2:15 am (utc) on May 8, 2009]
[edit reason] Disabled graphic smileys in code, example.com [/edit]

jdMorgan

12:48 am on May 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You put the leading slash back into the RewriteRule pattern, apparently... Take it out. In .htaccess, "^/archive..." should be "^archive..." as noted in my first post.

Jim

wedgin

12:52 am on May 8, 2009 (gmt 0)

10+ Year Member



Didn't end up sorting this out through htaccess. A friend pointed me to the Redirection plugin for Wordpress which has resolved the problem for me. Thanks for all the help, it was greatly appreciated.