Home > Back-end >  RewriteRule in htaccess returns the error Not found
RewriteRule in htaccess returns the error Not found

Time:09-04

I'm trying the second way to run PHP code in HTMl file using a rule in .htaccess file as described by the link https://stackoverflow.com/a/6237056/3208225

RewriteEngine on 
RewriteRule ^(.*)\.html $1\.php

But when I try opening my page e.g. test.html I receive

Not Found

The requested URL /test.html was not found on this server.

Why and how to resolve?

UPDATED (from comments)

I try both on localhost and on my shared hosting. htaccess and html files are in document root. BTW, the homepage (index.html) also returns Not found. In local machine the path is D:/Server/vhosts/another. And without this htaccess such virtual host works just fine. So there is no issue with its configuration.

CodePudding user response:

With your shown samples please try following .htaccess rules file. Make sure your htaccess is present along with your php files only. Also clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteRule ^(.*)\.html/?$ $1.php [NC,L]

Fix added by OP: Please note such rule is not execution of PHP code inside of HTML file, but just a redirecting from HTML file to PHP file, so the second file also must exist

  • Related