user1818027
user1818027

Reputation: 3

htaccess redirect ignored with rewriteengine

I have a htaccess question that I think may be so simple (dumb?) that no one has had to ask it before. I converted from using html pages to php and for the most part the following has worked:

<IfModule mod_rewrite.c>
 Options +FollowSymlinks
 RewriteEngine on
 RewriteRule ^(.+)\.htm$ /htmlrewrite.php?page=$1 [R=301,NC,L]
</IfModule>

There are a couple of exceptions though so I assumed putting:

Redirect 301 /oldpage.htm http://example.com/newpage.php

above the IfMofule... would do the trick but it doesn't. The "Redirect 301" is being ignored unless I remove the rest of the code. Could someone tell me what I'm doing wrong?

TIA.

Upvotes: 0

Views: 1402

Answers (2)

Jon Lin
Jon Lin

Reputation: 143906

You're using mod_rewrite and mod_alias together and they are both getting applied to the same URL. You should stick with either mod_rewrite or mod_alias.

mod_rewrite: (remove the Redirect directive)

<IfModule mod_rewrite.c>
 Options +FollowSymlinks
 RewriteEngine on
 RewriteRule ^oldpage.htm$ http://example.com/newpage.php [R=301,L]
 RewriteRule ^(.+)\.htm$ /htmlrewrite.php?page=$1 [R=301,NC,L]
</IfModule>

mod_alias: (remove everything in the <IfModule mod_rewrite.c> block:

Options +FollowSymlinks
Redirect 301 /oldpage.htm http://example.com/newpage.php
RedirectMatch 301 ^/(.+)\.htm$ /htmlrewrite.php?page=$1

Upvotes: 1

yasu
yasu

Reputation: 1364

Apache processes mod_rewrite (Rewrite*) first and then mod_alias (Redirect).

Use RewriteCond to prevent /oldpage.htm from processed by mod_rewrite.

RewriteCond %{REQUEST_URI} !^/oldpage\.htm$
RewriteRule ^(.+).htm$ /htmlrewrite.php?page=$1 [R=301,NC,L]

Or use RewriteRule instead of Redirect.

RewriteRule ^oldpage\.htm$ http://example.com/newpage.php [L,R=301]
RewriteRule ^(.+).htm$ /htmlrewrite.php?page=$1 [R=301,NC,L]

Upvotes: 0

Related Questions