Forum Moderators: phranque

Message Too Old, No Replies

Getting rid of?q=garbage.html in 301 redirections, how?

htaccess rewrites my redirections

         

Gusgsm

10:02 pm on Jan 3, 2007 (gmt 0)

10+ Year Member



Good evening and happy New Year,

I am using an .htaccess file to keep a set of permanent redirects after migrating from static html to a CMS with Drupal.

I have an .htaccess (heavily snipped) as such:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

redirect 301 /html/001.html [mysite.com...]

It worked properly making:

[mysite.com...] into [mysite.com...]

But after upgrading Drupal, the final URL becomes:

[mysite.com...] and that gives a 404 error.

If I coment the lines:

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

The redirection works to [mysite.com...] but the system runs amok and becomes unusable.

I am stuck. This must be pretty simple, but I don't get it: How can avoid htaccess rewriting my redirections in such a way, please?

Thanks a lot :)

[edited by: Gusgsm at 10:03 pm (utc) on Jan. 3, 2007]

jdMorgan

11:15 pm on Jan 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your example URLs aren't specific or consistent enough to really diagnose the problem, but one thing stands out: If you use two different modules to do rewriting/redirection, then you cannot control which module's directives will be executed first -- No matter what order you put them in the file.

Apache modules parse your .htaccess file(s) one-by-one, with each module handling the directives that it recognizes. Thus the execution order of directives addressed to different modules depends on the module execution order, controlled by the server configuration, not on the oredr that you place them in the file.

So first up, I'd change the code to use mod_rewrite only, and reverse the rules.


RewriteRule ^html/001\.html$ http://www.example.com/blah_blah [R=301,L]
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Again, I can't be sure because of the generic examples, but you might also have to change the redirect rule to this, in order to clear the query string if it's being passed from a prior php script rewrite:

RewriteRule ^html/001\.html$ http://www.example.com/blah_bl[b]ah?[/b] [R=301,L]

There are other potential causes for these problems, but I've addressed the most common ones.

Jim

Gusgsm

1:19 am on Jan 4, 2007 (gmt 0)

10+ Year Member



Excuseme, Jim but the example I am giving is consistent, as far as I can. I just changed the name of my site to respect the rules of the forum.

001.html is a real file that lives in a subfolder called "html", so [mysite.com...] is really real. And the [mysite.com...] is the typical url I have. Truly.

That's why I don't understand the change you point . Shouldn't it be (then)?

RewriteRule ^html/001.html http://www.example.com/blah_blah [R=301,L]

Sorry :)

jdMorgan

1:39 am on Jan 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The literal periods in regular-expressions patterns must be escaped by preceding them with "\", if that is the question.

"^" indicates that the requested URL-path must *start with* the character or pattern which follows it.
"$" indicates that the requested URL-path musr *end with* the character or pattern which precedes it.
RewriteRule cannot 'see' query strings appended to the URL-path, so a pattern ending with "\.html$" is usually used when URL-paths with .html extensions are being matched.

The meaning of blah_blah could be:

  • literally a path of "blah_blah"
  • <some_path> meaning "any path in this current directory level, but not below"
  • <some_path> or blank
  • <some_path>/<some_more_path> "any path in this directory or below" (or either of the two preceding)
  • <some_path>?<query_string>

    ...or any of a number of other possibilities. It is difficult to answer questions about generalized path descriptions, because mod_rewrite can handle all of these possibilities, and the code must correspond exactly to the one you really want.

    Sorry if I was or am being abrupt -- I often have to type fast to keep up.

    Jim

  • Gusgsm

    9:44 am on Jan 4, 2007 (gmt 0)

    10+ Year Member



    Thanks a lot, Jim. No hard feelings at all (on the contraryi :) We all have a job to do in between ;P

    I'll study this harder.

    Gusgsm

    7:22 pm on Jan 4, 2007 (gmt 0)

    10+ Year Member



    Jim, I'm back just to say 4 words: You are The Man!

    The perfect redirection was:

    RewriteRule ^html/001\.html$ [mysite.com...] [R=301,L]

    Thaaaaankkkk youuuu! :)

    [edited by: Gusgsm at 7:22 pm (utc) on Jan. 4, 2007]