Performance of PHP file_exists() vs. htaccess -f condition - php

I want to redirect to static html-files if they exist in a subdirectory.
I'm wondering if a little php script would be more performant as a htaccess RewriteCond.
Has somebody expiriences with that?
Thanks a lot.

Doing it via mode_rewrite (in httpd.conf or in .htaccess) will be more efficient than doing it via a PHP script.
Just think about it, doing it in PHP will require Apache to load mod_php module and then PHP interpreter will be called and your PHP script will be compiled and executed. All that can be skipped if you handle it in .htaccess itself.

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

What would cause Apache serving somepage.php/friendly-url yield 404 after PHP upgrade

My hoster has recently upgraded PHP to 5.3.8 and now all my installations that use a URL pattern like somefile.php/friendly-url yield a 404 ErrorDocument (where somefile.php is an actual existing PHP file which is supposed to handle the rest of the path). How do I best go about fixing this? What is the directive that tells Apache to serve said URL even if only the somefile.php part is valid but not the whole location does not really point to an existing file?
BTW: No URL rewriting is involved… Also, the 404 is clearly generated by Apache, not any scripts.
Try to add an .htaccess file to your public directory containing this line:
AcceptPathInfo On
From the PHP Manual:
Apache 2 users may use AcceptPathInfo On inside httpd.conf to define PATH_INFO.
Also have a look at the Apache2 documentation.
You probably had mod_rewrite configured differently before the upgrade... a similar thing happened to a sever I was working on about 2 months ago. Check out mod_rewrite and see if this is what you need.

How can I put Pyramid in front of a PHP website using the same webserver?

The scenario is: I current have an old website that runs on PHP. Over time, that code has become hacked up and messy. It's due for a rewrite. However, I don't have time to do that rewrite yet. But I'd like to plan for it in the future.
What I need to do now is add a 'welcome' type page to the website. I'd like to code that in Python using the Pyramid framework.
The old sites URL structure is this:
http://website.com/XXXXXX
Where the X is the short URL id.
What I was thinking of doing was using Apaches LocationMatch config to catch the short URL. Something like:
<LocationMatch "/^([a-zA-Z0-9]{6})$">
This would then redirect the request to the PHP portion of the website. Everything else would be caught by Pyramid.
I'm not sure how to action this. Does Apache have an else type clause for LocationMatch? How would I tell it to serve the PHP files for /XXXXXX matches and send everything else to Pyramid?
Server Notes:
Apache2 + PHP (Debian package)
mod_wsgi 3.3
Python2.7
I am not sure about Apache configuration, but you could use wphp, a wsgi middleware for serving php.
http://pythonpaste.org/wphp/
Use recipes for using AddHandler described in:
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive
Apply the AddHandler and the rewrite rule to your DocumentRoot directory. Then add the SCRIPT_NAME fixup to your WSGI script file.

Create a RESTful url for a php script

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.

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