I am trying to serve up an ics (ical) file via a url. Accessing my "ical.php" is fine. But many apps insist the url has a .ics extension. If I symlink "shamrock.ics" to ical.php then the raw php file is served up. I found a rewrite rule elsewhere in stackoverflow (RewriteRule ^(.*).ics$ $1.php [QSA]) but this doesnt work for me (other rewrites I use do work so I know the rewrite engine works).
Can someone suggest the next step or the best path to take to solve this.
I found the solution. And its not using rewrites. Simply add "AddType application/x-httpd-php .php .phtml .html .htm .ics" to the httpd.conf. I should have found that.
Related
A non-technical owner of an authority site with 1000s of asp/aspx pages decided to rebuild the site using WordPress on a new cPanel server. The original site was on a windows server and built with ASP.net. The problem is that they now had lots of dead external links going to *.asp and *.aspx pages that no longer existed.
I can get the static, mostly html content from the old site and wrap a php template derived from the WordPress template around it
I've added the following lines to .htaccess file so that asp and aspx files should be able to use php code, but this is not working.
AddHandler application/x-httpd-php .asp
AddHandler application/x-httpd-php .aspx
Any suggestions about getting php code to execute with files ending in .asp/.aspx on a linux host?
You can also set mime type handler using a RewriteRule
Try :
RewriteEngine on
RewriteRule ^.+\.aspx?$ - [H=application/x-httpd-php5]
x is optional in the pattern above ,so it matches both .asp or .aspx .
I was following a video tutorial on Youtube from phpacademy, https://www.youtube.com/watch?v=hxWYeCGa-PA&index=6&list=WL to make a dynamic RSS feed. In order to do that I need to be able to use php inside my rss file.
In this video the guy mentions adding the following line to the HTTPD-Conf file in order to allow this.
AddType application/x-httpd-php .rss
I contacted my web host 123-reg and asked them
Do I have access to the HTTPD.Conf file, or is it already configured to allow this.
I am looking to add the following line of code to the file if it isn't already in there.
AddType application/x-httpd-php .rss
What options do I have?
They responded with the following
The HTTPD.Conf file cannot be accessed with a shared hosting package
as the one used to host the website for the domain vwrx-project.co.uk.
This file is already configured to allow RSS, however you will need to
enable it. You can try adding the handler into the .htaccess file.
Does this make any sense to someone, I don't really know what code I should be looking to add into my .htaccess file
You can use url rewriting (if your hosting supports this) to direct non-existing files and directories, with .rss extension to .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.rss$ /$1.php [L,QSA]
Put all these lines into your .htaccess file and youtubefeed.rss will open youtubefeed.php, which you can program yourself.
After some more hunting around, and having looked up DaveG's code I wasn't sure it would do as I would have liked.
I have a file with a .rss extension for my rss feed, so from my understanding of the above code, it would redirect you from filnename.rss to filename.php where as what I was looking to do was retain the .rss extension but allow that file to read and interpret the php within it.
After some more searching I found that I could have used the original code from the video, and place it inside my .htaccess file instead on my httpd.conf file, I didn't realize that the code was interchangeable.
AddType application/x-httpd-php .rss // For older versions of php
Anyway whilst looking for this solution I saw some using the line of code below to achieve the same result, the only real difference being it is for PHP5 and later which is whats running on my server so AddType is now AddHandler and we add a 5 to the end of php
AddHandler application/x-httpd-php5 .rss // For use with PHP5+
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 recently played with a .htaccess file to make one server to parse PHP files. Yesterday I uploaded the same .htaccess file and tried to test a PHP file. But something went wrong: visiting my page the browser offers to download the the html page rather then viewing the page!
On the server the filenames end in .html.
I added the following to my .htaccess file:
AddType application/x-httpd-php .html
I tried to find the htaccess file, but once uploaded it just disappears from the root dir.
I tried to upload other scripts I've found browsing. I even tried to search for some problem on a hosting forum. Nothing helped.
Please help!
Try this
AddType application/x-httpd-php .php .htm .html
OR
AddHandler application/x-httpd-php .php .htm .html
And remove other overriding handlers for application/x-httpd-php after above code.
Right, firstly things first, what host do you use?
Also what ftp client you are using? Some by default won't display files starting with . such as .htaccess and .htpasswd, that's why it may appear that you didn't upload it. Also it might be that you don't have the rights to upload in the very root directory, try to go one directory up.
Also from my experience, hosts won't allow you to modify headers via .htaccess this way, because the allowoverride directive is off; instead have a look at url rewrites (via mod_rewrite), which allow you to do the same thing without modifying headers.
Your rewrite .htaccess file might look something like:
RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)\.html$ $1.php [NC]
(Not tested though)
Using a rewrite will also mean that your files will in fact maintain the php extension, however they will be access via urls that include .html extension.
Using Apache 2.2 and PHP 5, what's the best way to run PHP without the .php extension? For example, I have a script called app.php and I like to invoke it as:
http://example.com/app
Please notice that I still want to keep the .php extension to the file and I don't have mod_rewrite. Don't want use index.php either because it requires too many directories.
I did find one way by adding this to my .htaccess,
AddHandler server-parsed .php
SetHandler application/x-httpd-php
AddHandler application/x-httpd-php .php
The page runs a little slower by using this. I suspect it invokes SSI on every PHP page. Wonder if there are any better ways to accomplish this.
An alternative is to use content negotiation. Turn on multiviews:
Options +MultiViews
If a named resource doesn't exist, Apache will glob for the file, then sort based on the media type and content encoding requirements send by the browser. If there's only one file (your PHP script), then that's what the URL resolves to.
You could also force the mime type of a specific file in your .htaccess:
<Files app>
ForceType application/x-httpd-php
</Files>
The short answer is that you aren't going to be able to do this the way you want to. PHP is going to require SOME extension in order to execute the files so you might as well leave it as *.php.
Unless you use mod_rewrite you are going to have to call the files using the full file and extension.
That is the beauty of mod_rewrite--it lets you do just such a thing.
I would pose the question back to you--why can't you use mod_rewrite? Is it an environment issue, a choice, are you using Lighttpd (lighty)?
Extension
Wrap your rewrite rules in something like this to keep it from blowing up if the server doesn't support mod_rewrite:
<IfModule mod_rewrite.c>
// DO REWREITE HERE
</IfModule>
If you believe it to be a security concern or something equally valid then you could also do the following check and send them to a custom 404 or even your documentation and give them examples of how to enable mod_rewrite and explain the need, etc. This second check is optional though--if you don't use it the users will simply see the .php extension but your pages should still work.
<IfModule !mod_rewrite.c>
ErrorDocument 404 /application/errors/404.php
// ERROR 404 ABOVE OR DO ANOTHER DIRECT REDIRECT TO AN INFORMATION PAGE
</IfModule>