Create a RESTful url for a php script - php

I have a php script on a webserver. Currently, the script is being accessed as http://www.mydomain.com/scriptname.php .
Is there a way i can create a user friendly url for accessing this script, something like http://www.mydomain.com/appname, so when this url is called it invokes the php script ?
Please help.
Thank You

You want mod_rewrite if you're using Apache HTTPD: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
If you're using a different web server, it may have something similar (lighttpd has a similar functionality builtin).
Once it's enabled, you can use something like this in your .htaccess file to rewrite appname to scriptname.php
RewriteEngine on
RewriteRule ^appname$ scriptname.php

If you don't have access to your apache/lighttpd configuration file a little hack that may work is putting the script in http://www.mydomain.com/appname/index.php; http://www.mydomain.com/appname/ will then probably work.

Using the default settings of most PHP hosts, you can put the file "index.php" inside your folder http://www.mydomain.com/appname, then fill the index.php with next code:
<?php
header("location: http://www.mydomain.com/scriptname.php");
?>
That would do it.

Related

Accessing PHP Scripts Without .php Extension

How do you configure Apache and/or PHP to be able to access PHP scripts without the .php extension? I have seen PHP scripts executed without the .php extension. I don't mean executing 'script' as a PHP file, I mean executing 'domain.com/script' as a PHP file where 'script.php' exists as a file, but you are able to access it without using the extension. Does anybody know how to configure this?
I AM USING A CPANEL HOSTING!
WHERE TO WRITE THE mod_rewrite? I HAVE A .htaccess file with code # Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working RewriteBase /
Several basic ways:
Use MultiViews. Automatically converts /foo => /foo.php (among other things)
Use mod_rewrite to remove PHP extensions
Use mod_rewrite to direct all traffic to a single dispatcher script, which inspects the URL and performs the proper action by including files / calling class methods, etc.
Generally that's done in Apache via mod_rewrite.
Here's a guide: http://wettone.com/code/clean-urls
Such a rewriting doesn't make too much sense.
If you want (SEO-firiendly|human-readable) URLs, you have to use complete set of rewrite rules, not just removing extension.
Otherwise there would be no point in such a configuration change

Create SEO permalinks using PHP without .htaccess

Currently, my page URLs look this this:
http://ourdomain.com/articles/?permalink=blah-blah-blah
I want to convert these to:
http://ourdomain.com/articles/blah-blah-blah
How can I accomplish this using PHP but not with .htaccess?
How can i accomplish this using php but not with .htaccess..
You can't. You will need to tell the web server how to deal with URLs that don't physically exist. In Apache, that is done in the central configuration or in a .htaccess file.
If your server already happens to have AccepPathInfo On, you can try having URLs like
http://ourdomain.com/index.php/articles/blah-blah-blah
which will redirect to index.php and have articles/blah-blah-blah in the $_SERVER["PATH_INFO"] variable. This method is known as "poor man's URL rewriting" because you can't get rid of the index.php part in the URL. If the mentioned setting is turned on (I think it is by default), you may be able to do this without using a .htaccess file.
You can achieve this without mod_rewrite if you have access to the server configuration. Assuming you're using Apache, the first thing you would need to do is turn the MultiViews option on on your document root (ie. add Options MultiViews). Now copy your /articles/index.php to /articles.php (so put the script in your document root and rename it), and adapt your script so it reads $_SERVER["PATH_INFO"] to fetch the correct page (this of course relies on having AcceptPathInfo On).
MultiViews will make sure that the articles.php script is called when you provide a /articles/blah-blah URL.
I don't think you can easily do it without altering .htaccess. You'll most definitely need to use mod_rewrite. See the answers here for more info:
Special profile page link like www.domain.com/username
It is possible to do it in PHP, without modifying .htaccess
Just write following code in either index.php or default.php
<?php
if (isset($_GET['permalink'])) {
header('Location: '.urlencode($_GET['permalink']));
}
?>
It works because when you type following URL:
http://ourdomain.com/articles/?permalink=blah-blah-blah
The filename is not specified.
So, the server looks whether "index" or "default" file is present in the specified directory.
Consider file index.php is present, so server will call:
http://ourdomain.com/articles/index.php
with blah-blah-blah in GET variable permalink
The PHP code checks if permalink GET variable is present, and redirects using header() method.
EDIT: added urlencode() to do input validation

Protect files in directory using authentication script in php/apache

I'm looking for a way to tell Apache that if there is a request for a file from a certain directory it should first run a php script to verify if the user is logged in.
I know I could put the directory outside of the docroot and let a php script handle the authentication and file downloads, but because these are flash files that try to open other flash files it has to be a directory in the docroot, and the files should not have to be send by the php script.
In the old setup we were using mod_auth_script(http://sourceforge.net/projects/mod-auth-script/), but as that is a rather obscure apache module I'd rather have a more common solution if possible.
You can use .htaccess and mod_rewrite to redirect requests to php script. Try some googling and you will find lots of examples.
.htaccess contents example:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ([0-9a-z-_]+)\.swf$ checkForAuth.php?&file=$1 [L]
This will call checkForAuth.php when someone will try to access *.swf file. In checkForAuth.php you need to check your session, read contents from $_GET['file'], set correct headers (content-type for flash) and output contents of requested SWF file.

Converting a .php file to a .html file?

I have a .php file which has several queries in it. I want the output file as a .html file...
Is there any way to do this. I am currently doing this by saving, using my browser, that executed PHP file, as an .html file. But when I launch my product that should not be the case of the client.
Please suggest a way, thanks in advance...
Here is a sample code:
<?php
ob_start();
// your PHP / HTML code here
file_put_contents('where/to/save/generated.html', ob_get_clean());
?>
Not sure if I understand the question right.
But you can generate html files from php over command line:
php index.php > index.html
Another option is to use apache's mod_rewrite or the IIS equivelant to rewrite your URL's from the browser perspective.
This would require no coding change, make sure the Apache extension is installed and add this to the .htaccess file of your root web directory:
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]*)\.html$ $1.php?%{QUERY_STRING} [NC]
you can use output buffering (ob_start, etc.) and then write the content of the buffer to a file at the end of your script
Is this (this) what you need?
If these are not options, you can always curl to the page if it's running on a web server/
It looks like you want to save a static version of a dynamic web site. A tool like WinHTTrack comes in handy.

What happens first? .htaccess or php code?

If I use mod_rewrite to control all my 301 redirects, does this happen before my page is served? so if I also have a bunch of redirect rules in a php script that runs on my page, will the .htaccess kick in first?
The .htaccess will kick in first. If you look at the Apache request cycle:
PHP is a response handler. mod_rewrite runs at URI translation, except for rewrite rules in .htaccess and <Directory> or <Location> blocks which run in the fixup phase. This is because Apache doesn't know which directory it's in (and thus which <Directory> or .htaccess to read) until after URI translation.
In response to to gabriel1836's question about the image, I grabbed it from the second slide of this presentation but it's originally from the book: Writing Apache Modules in Perl and C which I highly recommend.
When a request is made to the URI affected by the .htaccess file, then Apache will handle any rewrite rules before any of your PHP code executes.
Yes, the .htaccess file is parsed before your script is served.
.htaccess happens first.
htaccess is controlled by the webserver. This file will be taken in account before your PHP file.
For example, you could restrict access to a particular folder with your htaccess file. So, it have to be take in charge before your PHP.
Hope this helps.
The .htaccess is performed by Apache before the php script execution.
(imagine if the php script is executed and then the .htaccess make a redirection to another page...).
You always can test this with the following command:
wget -S --spider http://yourdomain.com
With this command you see the who is responding to your request.
As all the others mentioned, .htaccess is first.
So basically, the .htaccess more or less requires the relevant PHP code or files, as according to the rules specified in the .htaccess, meaning .htaccess is run first.

Categories