How do I allow php files to be loaded with different extension? - php

I have some PHP files like index.php, contact.php, etc.
I want to name the files with the extension .blah (i.e, index.blah, contact.blah, etc. )
Is there something I can add to .htaccess to get them to load properly?

have a look at your httpd.conf to see how mime type handling is done for .php files, and simply do the same thing for .blah, or use mod_rewrite to change urls ending in .blah to internal redirects to .php through an .htaccess file. Either way, Apache's documentation is going to have all the details.

Related

How can I remove .php extension while using ISPCONFIG 3?

I'd need to remove the .php extension from the browser, I used to do it from apache2.conf in the last server I was running the website on, but on this new one I need to use ISPCONFIG3 and I don't know how to use set it to remove the .php extension, since the website was already running on a server which was rewriting the extension the links in the html are all without .php which obviously causes on this new one that pages are not loaded.
Many thanks in advance
You can solve this with mod_rewrite which is available by default in ISPconfig.
As admin you can add these RewriteRules on the Options tab of a web domain. But you can also store them in a .htaccess file together with the php files.
The needed rules are already explained nicely on put links without file extension (.php)

mod rewrite - img paths without extensions

I am using mod rewrite to strip URL of the .php extension, but the client wants me to go even further and strip of extensions all src paths in <img>. Is this doable via .htaccess, or maybe php? The only way I can think of now is JS but he wants it server side.
Add a .htaccess file with the following line:
Options +MultiViews
Then you can access the files within a directory without the extension, as long as your filenames are unique it should work OK.
The .htaccess file should be placed at the lowest level within your file structure that you want this behaviour to occur.
This will work for all extension!

PHP isn't working in file without extension

I had this setup:
images
image1.jpg
image2.jpg
header.html
about
index.php
image3.jpg
But going to MyWebsite.com/About gave it an extra slash on the end. I decided to go with the solution of creating a file called about in my home directory:
images
image1.jpg
image2.jpg
header.html
about
aboutfiles
image3.jpg
The problem is that now this file won't let me use .php:
<?php include('header.html');?>
It's not showing the header file. What can I do to make this work?
Your problem is that your web server does not recognize the filename "about" as a php document. You have three options
Use a ".php" extension on your page document, such as "index.php" which will run your php scripting.
Install a solution such as mod_rewrite that will translate urls such as /about to a file actual like "about.php".
Adjust your servers mime-type for php documents. Learn more about MIME types here. https://developer.mozilla.org/en-US/docs/Properly_Configuring_Server_MIME_Types
The first solution is the simplest and easiest, in any document you use PHP, the extension should be .php
About the mywebsite.com/about issue, it's more an Apache configuration issue. You have to tell Apache that index.php should be an index file.
First, you can configure your Apache (or IIS) to use whatever Extensions to process PHP-Code.
You can define .ThisIsAPHPFile as valid extension, if you want.
However, Directorys are always reflected with a trailing /: www.example.com/dir1/ (Browsers not always showing the trailing /) while files have an extension: www.example.com/dir1/index.html.
So, from what i see, you want to use www.example.com/about but showing the about-FILE ?
Therefore you can use rewrite Engines of your Webserver. Either have a look at mod-rewrite (http://httpd.apache.org/docs/current/en/mod/mod_rewrite.html), when using Apache, or (one possibility) ISAPI-Rewrite (http://www.isapirewrite.com/docs/), when using IIS.
If you edit your virtualhost directive in your apache conf file, you can add the following:
DefaultType application/x-httpd-php
This will tell apache to send web paths that do not end in an extension to render using the php engine.
Therefore, /about would act as if it was about.php. Another potentially more useful approach is to name the file about.php on the server, and allow referencing it without the .php in the url. For this, you would configure it the opposite way.
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

htaccess downloading file instead of loading

I would really like my index.html to be able to have a PHP script work on it. I read that you can do this through the htaccess file. I only have access to a subdomain website directory, where I can upload my files through FTP.
The directory did not have a htaccess file, so I created one using notepad: .htaccess and added this to the file:
AddType application/x-httpd-php .html
The problem is, instead of loading the index.html page, it downloads it as a file...would I need to add something extra to the htaccess file? :S
You don't need to name the file index.html to have it served by default. You can change the default document using your with an entry in your .htaccess file like this:
DirectoryIndex index.php
Then when you navigate to http://yoursubdomain.example.com you will be served index.php instead of index.html.
If really do want PHP to interpret your .html documents then the entry you had in your question will work when PHP is running as an Apache module. If your host is running PHP as CGI, you want:
AddHandler application/x-httpd-php .html
If it still doesn't work, then this web page has some more suggestions:
http://www.velvetblues.com/web-development-blog/how-to-parse-html-files-as-php/
The directive you have sets the content-type of files with a .html file extension.
If the server has PHP installed and enabled, that content-type will cause it to be run though the PHP engine and then the output from that sent to the client.
If it doesn't have PHP installed, then the file will just be served up to the client with that content-type. Since browsers don't handle PHP scripts themselves, they will then just save the file.
You need to install and enable PHP as well as setting the content-type.
Presumably your hosting is supporting PHP?
If so, then you need to rename your file from index.html to index.php

Is it possible to have a php file in apache operate without an extension?

I have a file
http://www.www.com/somefile.php
I would like to be able to get to the same file by going to
http://www.www.com/somefile
But I still need
http://www.www.com/directory
to open the file
http://www.www.com/directory/index.php
if, in fact, directory is a directory.
This is a typical LAMP setup with Red Hat and Apache.
Is that possible? If so, how?
An apache rewrite rule will do this for you. The only limitation is you can't have a file named "somefile.php" and a directory named "somefile" (though you can still have directories.
Here's an example rule that supports multiple filenames (just put in in your .htaccess file).
RewriteEngine on
RewriteRule ^(somefile1|somefile2|somefile3) $1.php
Will change /somefile1 to /somefile1.php, /somefile2 to /somefile2.php, and /somefile3 to /somefile3.php.
Look at Apache's ForceType.
If you want to disable the need of file extensions on the url globally on your site, you can also add the following to your .htaccess:
Options +MultiViews
This will enable Content Negotiation. But if you want to match only certain files and/or urls, stick with SoapBox's Answer instead.

Categories