Running other file types as PHP - php

Is there any problem with running HTML as PHP via .htaccess? such as security or best practices etc. was doing this to make URLs cleaner.
## run the following file types as php
Addhandler application/x-httpd-php .html .htm .rss .xml
Well ideally id like to have my URLs like
localhost/blog/posts/view.php?id=64
to be
localhost/projects/bittyPHP/bittyphp/posts/view/id-64
But having trouble accomplishing that without routing everything to one file and having PHP run determine the paths. I guess this is my real question

I would use mod rewrite.
Probably you do not need to run all html files as PHP, and if you have short_tags enabled "<?" in XML will give you trouble.

Keep in mind that you will run each and every of those files through the PHP handler then. If there is no PHP inside the files, the parser will still inspect them to see if there is any PHP in it. This adds some overhead, but it is likely neglectable in most setups.

Main issue I would say is performance. If you have a significant number of plain HTML files then you're creating unnecessary overhead by always running them through the PHP interpretter.
Best practice is not to do this, but use "friendly" URLS like mysite.com/item/123 and use mod_rewrite to convert them to mysite.com/displayitem.php?id=123 internally

Like many people have already stated, mod_rewrite is the best solution for accomplishing friendly URLs.
Sitepoint has a decent guide to getting started with mod_rewrite.

Related

Difference between .php extension and AddType

Since I want to have PHP code run properly on my website, should I add
AddType application/x-httpd-php .html
to my htaccess file, or just change all of my *.html files into *.php files?
I've heard that changing the file extension to *.php causes the website to load slower, but I'm wondering if changing the htaccess file does the same.
Either way, the files will be passed through the PHP interpreter, making them ever-so-slightly slower than if they were plain HTML files directly served down. It's the same process however you set it up. The difference in speed from plain HTML is going to be quite small unless you have a lot of dynamic PHP in there. Given that you are considering renaming existing files from .html to .php, I suspect you don't have much PHP code in there already (or any).
So it doesn't really matter which way you handle it.
However...
Leaving them as .html has the possible disadvantage that if you ever forget to setup this configuration, you could wind up serving raw PHP code to the browser, which might include your database connection details or other secrets.
it does exactly the same. .php is not slower than html, html is not slower than php, just a different setting in your webserver config.
AddType application/x-httpd-php .html would be a fraction slower, as apache load this line dynamically. If you set it in httpd.conf it would be exactly the same.
Agree with Michael that you need to be careful with renaming them HTML and having the chance that it not be setup, or your host provider do something screwy with your account.
If you do this, make sure any database/password files remain as PHP that you simply include in your HTML file.

Site Converter - Website Copier

Does anybody know of a software program that will convert a website built with PHP, JSON and jquery into a mainly HTML format. We need to do a conversion for SEO purposes and don't want to have to rewrite the whole site.
HTML is a language used for markup, PHP is an object oriented functional language. You cannot convert one to the other, I'm sorry.
If you're trying to make sure that you have nothing but .HTML extensions on your public URLs for SEO purposes:
Someone's selling you a line of BS.
You need access to your server configuration.
You don't have to convert anything but your links.
The .PHP extension is the default file extension configured to be sent from Apache to the PHP engine for parsing. You can change what file extension gets parsed in your configuration file.
http://encodable.com/parse_html_files_as_php/
This will allow you to keep .HTM files static and have .HTML files parsed as if they were .PHP files.
Try this: http://www.httrack.com/
It will only return a static HTML site. But it might be a good base for you.
Since the only thing which really knows what type of file you're using is the server itself, it does not really matter what you're using on the back end. Most search engines are smart enough to know that so they don't really care so much. Now, people might care. People might say, "Hm, well, this is .html, that means that this person must have a flat file which is constantly being updated," but I doubt it.
If you're really concerned about having a .html extension, then you can fake it by using htaccess:
RewriteRule ^(.*)\.html$ $1.php [L]
If that is placed in a .htaccess file at the root of your site, it will redirect all requests which end with .html to a corresponding page with .php. It will do that transparently both to the user and to the crawlers.
Of course, every link on your site will need to convert from linking to .php, but it will replace the impossible task of using only .html files with the annoying task of replacing all of your .php links.
As to removing JavaScript, well, you could do that, or you could design your site in such a way that it still uses AJAX but it works with the search engines instead of against them. The biggest trick is to make sure that your site can work with as little AJAX as possible and then use AJAX to supplement. We've come a long way from requiring that all websites work in lynx, but it is still good practice to make sure that they are still sane without the benefit of JS/CSS.
Besides, search engines are getting smarter. Google has been working to read AJAX intelligently since 2009. But even if they weren't, there are plenty of articles out there on using AJAX without hurting SEO.
There is no need to nerf your site because of SEO -- You can have your AJAX and SEO too.
This is hard to accomplish if there is a lot of dynamic data. For a simple website you can just cache every page and make that your new website. I am not sure how useful that would be. For example if you have forms or other user input fields then things will just not work. In any case this is how you do it using wget.
$ wget -m http://www.example.com/
More reading here.

Apaches mod_rewrite VS. PHP routing?

for some days now I have been trying to make a simple mod_rewrite rule to create friendly URLs, my web host have mod_rewrite enabled but I just can't get it to work.
All the next questions where posted by me:
.htacces to create friendly URLs
Friendly URLs with .htaccess
.htacces NOT working…
None of the answers worked, so I'm thinking now using simple php routing instead and I wanted to know if there is a big performance or SEO difference between the two. And if there is, maybe you know how to fix the mod_rewrite problems posted in my questions.
Thanks.
If you're using PHP routing for PHP files only, it would be no problem performance-wise: The interpreter will get started anyway, a new process started, memory allocated etc.
But if you are planning to route requests for static resources like images and style sheets as well, however, don't use PHP routing under any circumstance. It's way too resource-intensive and not what PHP was built for.
I'd say mod_rewrite is the better, leaner solution and it's worth trying to figure it out.
I prefer routing that kicks in when the requested file doesn't exist, like this in Lighttpd:
server.error-handler-404 = "/index.php"
Provided you find out how to do this in Apache, your script would be more cross webserver compatible, since Apache's mod_rewrite conditions in .htaccess won't work on Lighttpd.

PHP Dynamic Folders

Using PHP, how can you make a dynamic folder system, like http://website.com/user/UsernameHere/ show something like http://website.com/user.php?name=UsernameHere
Does this require server configuration, or is it possible with PHP?
Typically this will rely on some amount of URL-rewriting support from the web server, for instance, some mod_rewrite rule under apache.
Whether or not it's a bunch of complicated rewrite rules, or just a simple one that rewrites everything to /index.php (and let's index.php parse and interpret the URI), doesn't matter.
But you'll need some amount of web server configuration to get it going.
There's a few techniques for doing this. It's nearly possible to clean up your URLs using just PHP, but ultimately if you want to get rid of the ".php" extension you'll need to do something with your server. If it's Apache you can clean up using the .htaccess file.

PHP MVC Framework: Extension of View files

I have been developing my own PHP MVC framework. Now I have seen different frameworks implementing different extensions for the View files. I am using simply php extension for my view files.
Now is there anything wrong if i use php extension.
Are there any merits or de-merits of it?
Why use other extension such as:
phtml
etc
If you're talking about using these extensions in public facing URLs, then I would say don't use either:
File name extension. This is a very
common one. "cgi", even ".html" is
something which will change. You may
not be using HTML for that page in 20
years time, but you might want today's
links to it to still be valid. The
canonical way of making links to the
W3C site doesn't use the extension.
(Taken from W3C URL style guide)
You can achieve this with mod_rewrite, for example.
However, if you're talking about how to name your files in the filesystem, it's largely a matter of taste. I think both the extensions you suggested (phtml and php) make sense, the main thing is being consistent.
Edit: Also, since you said this is for a framework, you should consider choosing a non-standard extension may require extra webserver configuration. For example, to support both .phtml and .php in Apache:
AddType application/x-httpd-php .phtml .php
There's nothing wrong with using PHP extension if the code inside is valid PHP. It's nice to indicate somehow that a file is a view script. That's why some use .phtml. But I guess, you put them in a separate place-for-views anyway, right?
A benefit of .phtml is that it's obvious what kind of file it is when displayed in a "Jump to file" list. It's a feature of my IDE I use a lot: just typing a part of any file name in a project and picking the one to jump to.
It really doesn't matter what you use and as far as Apache goes its exactly the same if you are directly including the files.
Some use .tpl, some use .php and some use .phtml. Just pick the one you like the look of most.

Categories