Frank Nwoko
Frank Nwoko

Reputation: 3934

Unwanted .html extension after mod_rewrite rule

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^readnews/([0-9]+)\.html$ readnews.php?news_art_id=$1

This works but messes up my images.

The images folder is in a directory before the url http://localhost/newsdev/images but after re-write I get http://localhost/newsdev/readnews/123.html.

Please how can I also rewrite to remove the .html?

I would like to have http://localhost/newsdev/readnews/123.

Upvotes: 0

Views: 351

Answers (2)

Álvaro González
Álvaro González

Reputation: 146558

The expression you've posted should not be matching pictures. It's possible that you've misread the symptoms.

My educated guess is that you are liking images with a relative path. Since you are relocating the HTML document, you need to fix the paths accordingly.

  • images/foo.jpg from /newsdev/readnews.php is /newsdev/images/foo.jpg [X]
  • images/foo.jpg from /readnews/314.html is /readnews/images/foo.jpg [OK]

Or you can simply use absolute paths:

  • /newsdev/images/foo.jpg is always /newsdev/images/foo.jpg

As about removing the .html suffix, it doesn't exist until you put it there yourself. Just don't add it:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^readnews/([0-9]+)$ readnews.php?news_art_id=$1

Upvotes: 0

Poelinca Dorin
Poelinca Dorin

Reputation: 9703

This will not rewrite if the request is a file or a directory on the disk

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

To remove the html at the end you could use

RewriteRule ^readnews/([0-9]+)$ readnews.php?news_art_id=$1

Upvotes: 1

Related Questions