Accessing PHP Scripts Without .php Extension - php

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

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)

How do you write a web script to deal with urls of the form example.com/data/234 rather than example.com/data.php?id=234?

I'm use to working with $_POST and $_GET arrays in PHP scripts. I'm trying to learn Backbone (specifically the Backbone.sync function) and apparently it assumes that you'll set up your server so that example.com/student/32053 retrieves the student with id 32053.
How do I set up my scripts to do this?
Doesn't example.com/student/32053 load up index.* from the "32053" subdirectory of "student"?
On an Apache server, use the mod_rewrite module. In you .htaccess file, just specify a simple rule like:
^data/(\d+)$ data.php?id=$1 [L]
Doing this, when calling example.com/data/100, apache will internally call example.com/data.php?id=100.
Let me know if you need a similar example for Microsoft IIS.

How to treat a php file as directory

I wanted to know if it is possible to treat a php file as a directory so that index.php/abc/def really calls index.php. The index.php should then know the subdirectory path (ie /abc/def).
I'm searching a plain php solution. I know that I could use mod_rewrite to map the directory to GET-parameters.
If I recall correctly, you should be able to parse out this info from $_SERVER['PHP_SELF'].
Edit: Even better, take a look at $_SERVER["PATH_INFO"].
This will work if Apache's AcceptPathInfo directive is turned on, which is the default. From the manual:
The treatment of requests with trailing pathname information is determined by the handler responsible for the request. The core handler for normal files defaults to rejecting PATH_INFO requests. Handlers that serve scripts, such as cgi-script and isapi-handler, generally accept PATH_INFO by default.
you can query the path entered using the $_SERVER["PATH_INFO"] directive. You'll have to parse the paths yourself inside the script.
A plain PHP solution is impossible since what is called will be the file.
The PHP in the file does not care where it is or how the file it is in is called other than for include purpose.
What you are looking for is to either convince the OS to pass the /abc/def/ as a parameter to the script or to get the Webserver to do the same thing (ie. mod_rewrite for apache).

How to remove all .php extension from a dir and all of its subdirectories?

My web is dir structured is as follows :
moving/
includes/
controllers/
includes/
etc...
I have the following questions about /moving and all of its sub-diretories:
How can i request any php script without the .php extension?
How can i request ...dir/index.php?q=about as just ...dir/about?
Where woul i place my .htaccess file?
I understand that my first question might be a duplicate, but in order to contextualize my second and third question i had to ask it.
Using WampServer 2.0
Thanks in advance.
apache will not parse your php script if they don't have the ".php" extention.
but, you can use mod_rewrite:
This module uses a rule-based
rewriting engine (based on a
regular-expression parser) to rewrite
requested URLs on the fly. It supports
an unlimited number of rules and an
unlimited number of attached rule
conditions for each rule, to provide a
really flexible and powerful URL
manipulation mechanism. The URL
manipulations can depend on various
tests, of server variables,
environment variables, HTTP headers,
or time stamps. Even external database
lookups in various formats can be used
to achieve highly granular URL
matching.
This module operates on the full URLs
(including the path-info part) both in
per-server context (httpd.conf) and
per-directory context (.htaccess) and
can generate query-string parts on
result. The rewritten result can lead
to internal sub-processing, external
request redirection or even to an
internal proxy throughput.internal proxy throughput.
If all your website files are ending with .php and you want to take off the .php extension, then you should create and .htaccess file, or if you already have it, add this rule:
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
Supposing your website pages are let's say site/about.php, site/contact.php, site/news.php, the rule above will just transform those to site/about, site/contact, site/news
Hope it helps!
Provided you have mod_rewrite routing non-php extension request to the proper request, you would put it in your root web directory.

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