First of all, I am not trying to run php within a js script, there is a similar question on here that refers to a user trying to run php from inside a js script.
I have added many combinations of
AddType application/x-httpd-php .php .html
AddHandler x-httpd-php .html
to the .htaccess file in a higher level directory containing the .html file I want to run php in. This has not worked. (I am open to trying new combinations)
The is either not read at all or commented out when viewing the source in broswer.
My question is how to get to be run inside of an html file OR is there a better way to include php functionality in an html document without having the code in the same document.
Additionally my host uses cpanel if this helps anything.
I can elaborate on anything I need to, thanks in advance.
You can try to use mod_rewrite for that task:
RewriteEngine On
RewriteRule ^(.*).html index.php [QSA]
Related
This question already has answers here:
Process HTML files like PHP
(5 answers)
Closed 7 years ago.
I am totally new to php coding and my teacher told us that PHP files should be renamed with .php extension for it to work. But I have files in the serve with .htm extension, which I used with Google Analytics and Search Console, so now I don't want to change the extension to all my 5 five pages as I would make a huge chaos out of it. So is there a way I can insert php code into my html pages and still use it with .htm extension?
Thank you!
You want to write PHP codes within the .HTM / .HTML files
all you have to do is the add the following lines to your httpd.conf
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
note: restart your apache or rehash it so that the configurations take effect
Yes, you can use php code inside .html files, But you need to config the websever. In Apache you can config like this:
# Interpret both .php & .html as PHP:
AddHandler php5-script .php .html
As reference look this answer here
Hi you cannot run PHP withouth .php extension files. But you can work with friendly-urls.
You can do something like that
In your .htaccess file
RewriteEngine on
RewriteRule ^/example/([0-9]+)\ /example.php?id=$1
And this maps requests from
/example.php?id=contact
to
/example/contact
You can find more about it in Google typing "How I can work with url firendly in php".
Short answer: No.
Explained: The .php tells the server that the file has php information and needs to run the select code as php. Otherwise it'll run as normal html and won't run your code.
You can link to a separate .php page when something requires the php code. Yet if it's for a page with dynamic loading then you just have to change the extension.
I am designing a website that requires me to show the outputs of a mysql database on a webpage written in html. However my browser doesn't seem to recognize the php scripts within the html file and returns blank values where their should be details. The scripts worked fine in the .php files but have no effect when included in the .html files even after creating a .htaccess file as instructed in previously asked questions. Is there anything else that can be done to solve this issue?
There are two methods (that I've encountered) of changing the environment on an Apache server via .htaccess to allow PHP scripts to be processed in .html files, depending on the configuation. Whichever of these you've attempted, try the other.
AddHandler application/x-httpd-php5 .php .html .htm
Or
AddHandler cgi-script .html
SetEnv PHP_EXTENSION .html
I've searched all over the internet and can't seem to find a solution to my problem. I want to be able to "call" a php file from an html file and display the string returned:
html_ONLY_file.html
...
<h1>GetHeader.php?type=main</h1>
...
GetHeader.php
if($_GET['type'] == 'main')
print 'Some header to display'; //or echo 'Some...';
exit;
I've done this for images, img src="image.php?file=file.jpg", where image.php does a header(...) and readfile(...) return but I do not know how to do this for simple text. I'm not looking for DOM or anything too involved, if I have to I will. I want to know if there is a simple solution. It's generating the html side that I'm lost on.
In case you want to know, I am doing this because I once used a <#virtual include=...> to call for a php file from my .shtml file. Well, the hosting company decided to change mod_security and now I cannot include any php files (I've already tried everything). Including html files works fine and so I am changing this part of the website around so I don't have to rename files because the site is for a small business that is now ranked highly on Google for its geographical area. If I change the file names, shtml to php, then I believe the Google ranking drops (don't comment on this part unless you are damn sure you are 100% correct).
If you can edit the .htaccess file you can add a line in your .htaccess file that will mean HTML files will be parsed as if they were PHP.
If your server is running PHP as an apache module:
AddType application/x-httpd-php .html .htm
If your server is running PHP as CGI:
AddHandler application/x-httpd-php .html .htm
Source: http://www.besthostratings.com/articles/php-in-html-files.html
Once you've added that your html_ONLY_file.html could look like:
<h1><?php print "some header"; ?></h1>
And it would function just as if it were a .php file.
Alternatively, you could convert all your files to .php and add redirects into the .htaccess file like so:
rewriteRule ^somefile.shtml$ somefile.php [R=301,L]
rewriteRule ^another-file.shtml$ another-file.php [R=301,L]
This effectively says to your server "whenever a user requests somefile.shtml, act as if they requested somefile.php instead". The R=301 is the most important part with regards to Google rankings. This tells Google (and anyone who requests the .shtml file) that it's permanently moved to the new location somefile.php. This transfers all / almost all of the ranking power from the old location to the new location.
Source: http://moz.com/learn/seo/redirection
I have been trying to execute php code within a document with an .htm or .html extension. I finally got it working using:
AddType application/x-httpd-php .php .html .htm
Now, being able to execute php within .htm documents, only works if I go directly to the .htm page such as: http://www.foobar.com/layout.htm
However, it does not work if I go to the index.php page which uses that layout.htm page...
This is an example of what the index page url looks like: http://www.foobar.com/index.php
What am I doing wrong?
Assuming that you are using something like include in your php file to include the html file, your .htaccess rules will not have any effect on the included file.
The .htaccess rules only get executed on requests that are made to the apache web-server and when you include a local file in php, you are simply requesting a file on the local file system; you are not requesting it through apache.
Edit: Based on the comment below your question, it is also possible that you are using something like readfile to get the contents of the htm file. If that is the case, you need to change that to include so that the php gets executed.
Your .htaccess rule does not apply for included files in php.
Did you check this page: http://php.net/manual/en/function.include.php
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.
I have made a subdomain and uploaded php script on that. but php code is shown there included file are also not shown on page.
In description, i have made a sub domain jobs.example.com. All project done in php on locally when is uploaded it to the srever (in the root flder of jobs.example.com). only html css and jquery is running php code is shown there as it is. How may i handle this plese help me.
My guess is: may be you might need to set up your subdomain folder to run php files.
You might need to setup mime type for .php
# Add this line inside the <IfModule mod_mime.c> conditional brace
AddType application/x-httpd-php .php
Referenced from : http://www.php.net/manual/en/install.windows.apache1.php