Forum Moderators: phranque

Message Too Old, No Replies

.htacess RewriteRule Problem

301 Redirect results in "cannot display page" error

         

AsimJ

12:43 am on Mar 1, 2006 (gmt 0)

10+ Year Member



We are using Apache/1.3.33 Ben-SSL/1.55 (Unix) and for some reason cannot use the "R=301" clause along with [QSA,L] in any RewriteRule commands other than for the home page. Placing [R=301,QSA,L] on any of the other RewriteRule lines results in a "page cannot be displayed" error.

The [QSA] is required as we want variables to be displayed and indexed by search engines, e.g. index.html?substring=shaver

Without the 301 clause all redirections are flagged as 302 Temporary Redirects, which is highly undesirable in SEO terms.

Any help would be greatly appreciated.

---------- .htaccess ----------

DirectoryIndex index.php

AddType text/css .css
AddType text/x-server-parsed-html .html
AddType text/x-server-parsed-html .htm
Options -Indexes

RewriteEngine On
RewriteBase /

RewriteRule ^index.php$ gb/index.html [R=301,QSA,L]
RewriteRule ^(.*)/index\.html$ home.php?sl=$1 [QSA,L]
RewriteRule ^index\.html$ home.php [QSA,L]
RewriteRule ^(.*)/product/(.*)\.html$ product.php?productid=$2&sl=$1 [QSA,L]
RewriteRule ^(.*)/category/(.*)\.html$ home.php?cat=$2&sl=$1 [QSA,L]
RewriteRule ^(.*)/pages/(.*)\.html$ pages.php?pagecode=$2&sl=$1 [QSA,L]
RewriteRule ^(.*)/(.*)\.html$ help.php?section=$2&sl=$1 [QSA,L]

P.S I just signed up for subscription to the Private Forums but cannot access them.

jdMorgan

2:13 am on Mar 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



AsimJ,

Welcome to WebmasterWorld!

  • {QSA} is only needed if you wish to *append* additional query parameters to the query string present on the requested URL. [QSA] is not needed if you do not specify a new query string in the RewriteRule substitution, or if you wish to replace the request query with a new one.

  • The correct syntax for a 301 redirect, replacing any existing query is:

    RewriteRule ^([^/]+)/([^.]+)\.html$ http://www.example.com/help.php?section=$2&sl=$1 [R=301,L]

  • The correct syntax for a 301 redirect, appending your $2 and $1 variables to the originally-requested query string is:

    RewriteRule ^([^/]+)/([^.]+)\.html$ http://www.example.com/help.php?section=$2&sl=$1 [QSA,R=301,L]

  • If you wish to use 'search-engine-friendly' static URLs, then you don't want *any* external redirect, whether 301 or 302; An external redirect 'exposes' your dynamic help.php?query URLs to search engines, and this dynamic URL will then replace your static /abc/def.html URL in search engine listings. It is much more common to use an internal rewrite, rather than an external redirect:

    RewriteRule ^([^/]+)/([^.]+)\.html$ /help.php?section=$2&sl=$1 [L]

  • If you are using Internet Explorer for Web development purposes, then turn off "Show Friendly Error Messages" in the advanced IE options tab.

  • Supporters forum subscriptions are processed by WebmasterWorld staff during U.S. weekday office hours. If your subscription is not active after 24 hours, please use the "Report a Problem" link available at the bottom of each page on WebmasterWorld.

    Jim

  • AsimJ

    2:14 pm on Mar 1, 2006 (gmt 0)

    10+ Year Member



    Hi Jim,

    Thanks for the information, it was extremely useful and got me really thinking.

    I just checked the HTTP responses to the rewritten URL's on our site and they all come back with "HTTP/1.1 200 OK".

    If I understand correctly, you are saying that an internal redirect that returns a 200 response code is better for search engine optimisation than an external 301/302 redirect.

    jdMorgan

    2:38 pm on Mar 1, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    In most cases, webmasters do not want a 301 or 302 redirect to a dynamic URL. A redirect sends a response to the client (browser or SE robot) saying, "The content you asked for has moved. Ask for it again using this new URL." The client then must re-request the desired content using the new URL provided in the redirect rresponse.

    If you send a 301 or 302 redirect response to search engine robot, you must expect that it will delete the first URL that it requested, and replace it's database entry with the URL given in the redirect response.

    Since the goal of search-engine-friendly URLs is to avoid having search engines 'see' any dynamic URLs, redirecting to a dynamic URL defeats the whole purpose.

    My main point, however, is that many webmasters try to do this friendly-URL thing backwards, believing that mod_rewrite can change the URLs that appear on their pages. Since it is a 'URL-to-filename translator' process and not a 'page content output filter,' it cannot do this.

    Jim

    AsimJ

    7:26 pm on Mar 12, 2006 (gmt 0)

    10+ Year Member



    I have decided to remove URL masking and return to PHP URL's as it is causing more problems than it is worth.

    However, Google has already indexed the masked URL's.

    How do I reverse the following redirect and add a 301 clause?

    RewriteRule ^(.*)/product/(.*)\.html$ product.php?productid=$2&sl=$1 [QSA,L]