Query string in .html extension files - php

For some sites that have their backend on PHP/any other server side languages, the query string http://example.com/?param1=whatever makes sense. But I saw it is used with pages that have .html extension as well. How does that make sense? If we are explicitly writing the url to end in .html, it means it is referring to a file located on the server, and it is an HTML file, which cannot deal with server side stuff. So how does it make sense?
As an example, http://www.example.com/mypage.html?crcat=test&crsource=test&crkw=buy-a-lot1, this url is given on https://help.marketruler.com/wiki/What_is_the_correct_syntax_for_query_strings%3F link (under Landings to a page within your domain name with no query string).

There are two reasons:
You can fetch the parameters with JavaScript and use them with client side programming
You can change the Mime type of .html files and parse them as a PHP or other programming language script. For example in Apache to make html files to work like php scripts it is as simple as adding this to .htaccess file:
AddType application/x-httpd-php .html

Related

Why doesn't this html code work?

Given the following index.html file:
<html>
<body>
<p>Welcome to <?= $_SERVER ['HTTP_HOST']; ?></p>
</body>
</html>
I expect to see "Welcome to EXAMPLE.COM", All I see is "Welcome to ".
What would cause this to happen if the code checks out?
Your file is named index.html. Unless you told your server that .html files should be treated as PHP scripts, that means the PHP code is NOT being executed - it's going out as literal text. And since PHP tags make it look like HTML, your browser is properly hiding that unknown/illegal tag.
Rename it to index.php.
It's not running because that part is not html (it's php) but you have saved it as an html file instead of as a php file.
rename it to index.php and try again?
Web servers are usually configured to run PHP only on files with the .php extension. Your index.html file will be passed as-is to the browser, which will probably ignore the unknown PHP tags. If you take a look at the source code of the web page, the tags will probably be there.
If you must have a .html extension, you can usually configure the web server to run PHP on .html files. For example, in Apache, you can use the AddType directive in an .htaccess file or in the server configuration (httpd.conf):
AddType application/x-httpd-php .html
However, this will run PHP on all .html files (in that directory), which may put an unnecessary load on the server.
A much better way is to use URLs without extensions. In Apache, you can use the DirectoryIndex directive to specify a list of index files that the web server will search for:
Options +Indexes
DirectoryIndex index.html index.php
When a browser requests a URL that ends with a slash, such as http://mydomain.example/foo/, the server will search for foo/index.html or foo/index.php in the DocumentRoot (or, failing both, generate a directory listing). You can now use whichever type of index file is appropriate for the moment, without ever having to change your URLs.

HTML: Display a string from a separate PHP file

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

Include option in HTML

I'm working on a navigation bar similar to this website and google, and lets say i would like to make more sites with the same layout. How would i go about doing this with html? I know that php has:
<?php include "file name"; ?>
Is there a way to do something like that in HTML document?
HTML is not a server-side language, it's just a markup language. It does not offer any way to include the contents of a different page in your HTML code. HTML simply doesn't support it.
If you're willing to use a library such as jQuery, then this will be an easy task. You can use the jQuery .load() method to fetch the contents of your HTML file and then inject it the HTML DOM.
$(document).ready(function() {
$('#someDiv').load('/path/to/file.html');
});
Note that remote files won't work because of the Same Origin Policy. Also, note that it's a bad idea if you care about SEO. The search engine spiders won't see your actual HTML code, and they might miss out important parts of your website.
The best way to do this would be to use a server-side language. If you're already serving dynamic content, then it makes more sense to do so. Server-side scripting runs on the server machine and then the results of that scripting, generally HTML markup are then sent out to the client.
The difference is that the HTML markup is generated before pageload, not during or after, as is the case with the method shown above.
Just make your HTML files run through a PHP parser, I do that for all my sites so things like menus and other repeating sections can just be updated once. You do this with your .htaccess file.
This will make all .htm and .html files be run through the PHP parser as well.
<FilesMatch "\.(htm|html)$">
SetHandler application/x-httpd-php
</FilesMatch>
Then you just use the same method you mentioned in your original post. Whatever you include will be processed by the PHP parser, then sent to the browser, which will interpret the HTML.
Note that PHP files can hold anything in them, only the parts inside <?php ?> tags will actually be processed by the PHP parser. So I have section that are all HTML, except for one small section that has something like a year. The below is how my websites will never have their copyright be out of date, even if the website is not maintained.
<p>
Copyright<sup>©</sup> NAME. All Rights Reserved. <?php echo date("Y"); ?>.
</p>
"Is there a way to do something like that in HTML document?"
Yes there is an HTML equivalent (if I can say) to include files, and it's called Server-side Includes (SSI) which uses the .shtml file extension, however this is an Apache feature. Yet, from a recent finding, it can run on Windows servers. Consult Microsoft's Developer Network for more information on the usage of SSI and this page also on Microsoft's IIS.net.
The syntax is: (no space between the <!-- and the #include...)
<!--#include virtual="/includes/file.shtml" -->
This would look and fetch a file called file.shtml to the root and inside the "includes" folder.
Note: This can be any folder you want it to be.
You can also include different file extensions such as .htm .html .txt and even another .shtml file.
I manage a few Websites with that particular file extension, and the benefit of including .shtml files, is that you can also do more includes inside those, but not with the other file extensions I already listed.
However, there is an exception to this rule. You can tell the server to treat .html or .htm to run as .shtml just as long as you use the
AddHandler server-parsed .html
AddHandler server-parsed .shtml
AddHandler server-parsed .htm
Apache commands inside an .htaccess file and placed in the root of your server.
Back to the matter at hand. You can use it anywhere in the document you wish to include a file in.
For more information on Server-side Includes and other available options, visit the Wikipedia Website, or Google the term "Server-side Includes (SSI)".
http://en.wikipedia.org/wiki/Server_Side_Includes
<frameset cols="25%,50%,25%">
<frame src="www.google.com">
<frame src="www.facebook.com">
<frame src="www.something.ocm">
</frameset>
FRAMETAG is not supported in HTML5! Beaware
In W3C Validation it will throw an error, but in real it will work.

htaccess modifications working on some pages but not on others?

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.

what does .htaccess with line AddHandler php5-script .php do?

I am with new web host. The public_html folder of each domain I create is auto generated with an .htaccess that has the following line:
AddHandler php5-script .php
What is this for?
This just instructs PHP to handle files ending in .php by passing them to the PHP5 interpreter. Without this configuration in place, the web server may serve the files to the end-user's web browser as raw PHP code, rather than executing the code. That raises the dangerous possibility of exposing database login credentials or, or other secrets.
Using the same mechanism, you could configure the web server to parse files with other extensions besides .php as PHP scripts and hand them to the PHP interpreter. This is occasionally done to mask PHP scripts by naming them with .html extensions, for example.
# Interpret both .php & .html as PHP:
AddHandler php5-script .php .html
It tells php to handle any file with .php in the filename, even if it's not at the end. A file named smile.php.gif will be interpereted as a php file, which is bad if you are going to be using an upload script. This is because Apache allows multiple extensions in any order, so gif.php.jpg is the same as gif.jpg.php. I have heard the best way to select the handler is with FilesMatch. Of course if your web host has this in their httpd.conf you would have to 'remove' it using your htaccess before using the FilesMatch if you don't have access to httpd.conf.
The answer is that the htaccess tells the webserver to handle the php as php5-script and execute it.
Regarding the first answer, you will achieve your goal but it is a really bad practice and you should not allow html files to be executed as php due to huge security concerns.

Categories