1

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.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
stckvrw
  • 1,689
  • 18
  • 42
  • Could you please do mention which sample link you are hitting and by which file you want to serve it in backend? – RavinderSingh13 Aug 31 '22 at 12:09
  • @RavinderSingh13 I follow by the link like `http://my-domain.com/test.html` and my PHP code is `` – stckvrw Aug 31 '22 at 12:12
  • The substitution portion of the RewriteRule arguments is not a regular expression, so do not "escape" characters like the dot in there. – CBroe Aug 31 '22 at 12:48

1 Answers1

2

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

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • @stckvrw, ok where are your htaccess and php files present? Kindly do mention all path details here. – RavinderSingh13 Aug 31 '22 at 12:16
  • @stckvrw, Do you have any other rules apart from these in your htaccess file? If yes then please do let me know and update your question also. – RavinderSingh13 Aug 31 '22 at 12:20
  • @stckvrw, Also mention path of htaccess and php files like: `/root/singh/test/.htaccess` etc etc – RavinderSingh13 Aug 31 '22 at 12:20
  • No, htaccess has these two rules only – stckvrw Aug 31 '22 at 12:21
  • @stckvrw, usually `not found` errors are path problems only. So kindly mention path of htaccess and php files like: `/root/singh/test/.htaccess etc` – RavinderSingh13 Aug 31 '22 at 12:22
  • @stckvrw, ok could you please do check if your virtual host entries are fine Eg: ` ServerName site.ws DocumentRoot /home/me/Projects/website/build ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined AllowOverride All Require all granted `? – RavinderSingh13 Aug 31 '22 at 12:25
  • @stckvrw, ok next thing we can check is, is your htaccess is enabled or not. Try putting a dummy line like: `----------------------` in your htaccess and save it and try to HIT anyink if you get 500 internal error then it means htsccess is enabled else it's not. Kindly do try it out and let me know how it goes. – RavinderSingh13 Aug 31 '22 at 12:35
  • Yes, I get 500 error with the dummy line – stckvrw Aug 31 '22 at 12:42
  • Also I get 500 error when I add `RewriteBase D:/Server/vhosts/another` before RewriteRule – stckvrw Aug 31 '22 at 12:43
  • 1
    RewriteBase is supposed to be a URL path, not a file system path. – CBroe Aug 31 '22 at 12:48
  • 1
    @RavinderSingh13 Oh, sorry, I found the reason. I just thought that if I create test.html file with PHP code, the code in such HTML file will be executed even if there is no test.php file, like on the fly. But I was wrong. So the reason was, there is no the test.php file – stckvrw Aug 31 '22 at 12:51
  • @stckvrw, ok cool good to know. Could you please do add further details to my answer, please edit it and I will approve it. So that future users will be benefited from this one, cheers. – RavinderSingh13 Aug 31 '22 at 12:52
  • @stckvrw, no please edit my answer, as answers are not supposed to be there in question. It will make difficult for future users to understand moreover its not encouraged to do so in Stack, thank you. – RavinderSingh13 Aug 31 '22 at 12:56
  • @stckvrw, yes, I mean whatever was the fix of the issue, as simple as that, cheers. – RavinderSingh13 Aug 31 '22 at 13:03
  • 1
    @RavinderSingh13 Ok, so do I understand it properly: the only way to execute PHP code inside of non-php file is the rule like `AddType application/x-httpd-lsphp .html` ? (only this one works in my shared hosting) – stckvrw Aug 31 '22 at 13:12
  • @stckvrw, I apologise here, I am not sure on this part `the only way to execute PHP code inside of non-php file is the rule like`. May be you could discus in new thread on this one, Sorry I am really not sure on this one, cheers. – RavinderSingh13 Aug 31 '22 at 13:14