Problem is: I have a website let www.mydomain.com
and it loads index.html, in this file i write some php code but it is not executing
Some days ago i found the solution for executing php in .html file that simply just make a line in .htaccess: (works perfectly in .html files)
AddType application/x-httpd-php .htm .html
In my case above code not working, here problem is that in address bar my link index.html start as mydomain.com so as it is not representing file extension may be thats why above htaccess code not working.
Help me please.
Thanks in advance!
You can actually name your home file "index.php". There just can't be an index.html at the same time (unless editing some configuration stuff).
Perhaps an iFrame would work, which you place in index.html. You can place your PHP code in another page e.g. form.php and load that page in the iFrame. You'd have to do a lot of testing to make sure the functionality is given, as iFrames can cause all sorts of issues, especially, but not limited to, IE.
Related
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
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]
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.
Ok so I've recently installed to test Orbis CMS, and I love it, except two things - this being one of them.
Test page 'page.php' worked fine, great! Incorporated it into an existing html page and that's where I ran into the issue. Duhr, it won't run the php snippits.
Quick Google shed light that I needed to add a piece of code into the .htaccess file to allow the html file(s) to run the php code. Tried it, and nothing, didn't work, at all. Was going off this link (provided by Orbis) here: http://php.about.com/od/advancedphp/p/html_php.htm
When I did it for all pages it started doing funky stuff and asking me to download/open pages when I clicked the links throughout the site, but not every link.. ?? When I did it for the single test page it didn't work full stop.
Anyone got any ideas?
Cheers.
Why don't you just rename the .htm file to .php ? You can have normal HTML in PHP files aswell, just outside of the <?php ?>Tags.
.htaccess has to be allowed from your hoster, so that might be the reason why it didn't work.
EDIT: Then just replace the .htm File with a redirection to your .php file.
You can achieve this by <meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/yourfile.php">
Or if the .htaccess is run by the webserver you can do a redirect there:
Redirect /youroldsite.htm yournewsite.php (would be the better version)
You need to change your .html to .php.
as you mentioned that .htaccess is not working check these thing it may help you
Is .htaccess enabled?
It's unusual, but possible that .htaccess is not enabled on your site. If you are hosting it yourself, it's easy enough to fix; open your httpd.conf in a text editor, and locate this section
Your DocumentRoot may be different, of course
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/var/www/htdocs">
#
locate the line that reads..
AllowOverride None
and change it to..
AllowOverride All
Restart Apache. Now .htaccess will work. You can also make this change inside a virtual host, which would normally be preferable.
If your site is hosted with someone else, check your control panel (Plesk. CPanel, etc.) to see if you can enable it there, and if not, contact your hosting admins. Perhaps they don't allow this. In which case, switch to a better web host.
and also check that rewrite_module is marked
Rename your Files to .php or change your server configuration to recognize .htm files as PHP files, too.
Example (httpd.conf):
<IfModule php5_module>
AddType application/x-httpd-php .php .htm .html
</IfModule>
As an alternative you could setup mod_rewrite to route to your php file on specific URLs.
You need to change your .html to .php. You mentioned you need to keep the link equity from other sites to your .html pages. To get around this, simply add a 301 redirect in your .htaccess file.
//301 Redirect
Redirect 301 oldfile.html newfile.php
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