3

I'm pretty green with mod_rewrite. I've got the first line of this working like a champ, but the second part refuses to cooperate

RewriteRule ^(.*).html$ $1.php
RewriteRule ^(.*).shtml$ $1.php [R=301,L]

The .shtml part fails wonderfully and 404s - without my custom 404 page. Should this be combined in to one rule? Should I quit my job and let them hire a monkey to replace me? has anyone got a solution for this?

Thanks!

1 Answers1

2

Assuming you are intending to redirect both .shtml and .html to .php...

These should both be combined into one rule, and the problem looks to be that both match because the unescaped . before .html stands for "any character", which therefore also matches shtml.

The pattern \.s?html will match the literal ., followed by an optional s, and ending with html.

RewriteEngine On
RewriteRule ^(.*)\.s?html$ $1.php [R=301,L]
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • Ah! Your technical point makes sense. Thank you for the clarification. Your suggestion, however, fails for me. I get a redirection complaint from IE and FF along the lines of "The page isn't redirecting properly". any other ideas? – Amelia Earhart Dec 13 '12 at 20:13
  • @AmeliaEarhart You would have to look in the web server access and error logs. If it is entering a rewrite loop, that may be because of some other rewrite rule in this directory, or in another directory. As it is above, there's no way a .php request would match this rule and go into a rewrite loop. – Michael Berkowski Dec 13 '12 at 20:17
  • 1
    Do you want to actually redirect the browser to `.php`, or silently serve the `.php` script without the browser knowing about it? ( if silently, remove the R=301) – Michael Berkowski Dec 13 '12 at 20:18
  • Yup. That was it. I'd been messing around with .htaccess in a subdir - forgotten about that. Problem solved. Thank you, Michael. – Amelia Earhart Dec 13 '12 at 20:29