2

Trying to rewrite links that look like this:

/content/about-me

...to this:

/content/about-me.html

It's necessary for what I'm working on. I keep getting internal server errors with this htaccess rule:

RewriteRule ^content/(.*) /content/$1.html [NC,L]

Any idea what I'm doing wrong? Loads of examples on the net, but can't quite get it right.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
cimeran
  • 23
  • 2
  • Is that your whole .htaccess-File? Is mod_rewrite enabled? Have you checked out the Apache error_log (if you have access to it) for any error messages? – vstm May 15 '12 at 16:07
  • RewriteEngine On RewriteRule ^content(/.*)?$ /content$1.html [NC,L] – cimeran May 15 '12 at 16:24

1 Answers1

1

Your rewrite rule (pattern) is too broad and will catch already rewritten URLs. Overwriting already rewritten URL will lead to infinite rewrite loop, which Apache intelligently detects and aborts such request with 500 error.

RewriteRule Last [L] flag not working?

For example:

  • Original request: /hello/pink-kitten
  • 1st (initial) rewrite: /hello/pink-kitten.html
  • 2nd cycle: rewritten to /hello/pink-kitten.html.html
  • 3rd cycle: rewritten to /hello/pink-kitten.html.html.html
  • ... and so on

More correct approach (one of few possible approaches) -- will check if such html file exists BEFORE rewriting:

# add .html file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^content/(.+)$ /content/$1.html [L]
Community
  • 1
  • 1
LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • I thought it was rewriting already rewritten ones. Thanks so much for pointing me in the right direction. – cimeran May 15 '12 at 16:27