Enhance PHP Performance by Handling 404 Error Requests by Apache

0 17

Serving PHP requests are very slow when compared to serving Static HTML files, yes this is because PHP has to parse all files on each request unless you have an OPCODE installed like OPCache (PHP 7) or APC (upto PHP 5.4), If you are using a Dedicated Server / VPS you can install OPCODE caches and fasten up PHP and Apache but with Shared hosting you are very very limited.

HTTP 404 errors are caused if the file has been deleted or moved from server, if you have enabled pretty permalinks with htaccess by redirecting all incoming requests to PHP, all 404 errors caused by deleted/removed static files will be passed to index.php and request will be handled by CMS that you are using for an example WordPress, which puts a heavy load on server

Reduce server load by allowing the web server to handle 404 (not found) errors for static files (images etc).

If enabled – you may get 404 File Not Found response for some files generated on-the-fly by WordPress/CMS plugins. You may have add those file URIs to 404 error exception manually, add the following code to your .htaccess file

# Handling 404 Requests by Apache for static files || Guest.blog || Guest.blog/?p=3179
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_URI} !(robots\.txt|[a-z0-9_\-]*sitemap[a-z0-9_\-]*\.(xml|xsl|html)(\.gz)?)
 RewriteCond %{REQUEST_FILENAME} \.(css|htc|less|js|js2|js3|js4|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|woff|xla|xls|xlsx|xlt|xlw|zip)$ [NC]
 RewriteRule .* - [L]
</IfModule>
# Handling 404 Requests by Apache for static files || Guest.blog || Guest.blog/?p=3179

Well this is not enough, create a file with following content

<html>
 <title>404 Not Found</title>
 <body>
 The file you were looking for could not be found
 </body>
</html>

Save it as HTML file with name 404 (404.html) copy it to Document root (public_html), (download 404.html)
add the following directive to .htaccess just after RewriteBase

ErrorDocument 404 /404.html

Something that would be similar to

RewriteEngine On
RewriteBase /
ErrorDocument 404 /404.html

Thats all, most static file 404 requests will be handled by Apache instead of PHP application, by reducing 404 request to PHP, server load is dramatically reduced.

Pretty Permalinks

Permalinks are the permanent URLs to your individual weblog posts, as well as categories and other lists of weblog postings. A permalink is what another weblogger will use to refer to your article (or section), or how you might send a link to your story in an e-mail message. Because others may link to your individual postings, the URL to that article shouldn’t change. Permalinks are intended to be permanent (valid for a long time).

“Pretty” Permalinks is the idea that URLs are frequently visible to the people who click them, and should therefore be crafted in such a way that they make sense, and not be filled with incomprehensible parameters. The best Permalinks are “hackable,” meaning a user might modify the link text in their browser to navigate to another section or listing of the weblog. For example, this is how the default Permalink to a story might look in a default WordPress installation:

/index.php?p=423

How is a user to know what “p” represents? Where did the number 423 come from?

In contrast, here is a well-structured, “Pretty” Permalink which could link to the same article, once the installation is configured to modify permalinks:

/archives/2003/05/23/my-cheese-sandwich/

One can easily guess that the Permalink includes the date of the posting, and the title, just by looking at the URL. One might also guess that hacking the URL to be /archives/2003/05/ would get a list of all the postings from May of 2003 (pretty cool). For more information on possible Permalink patterns in WordPress, see Using Permalinks.

Reference: Pretty_Permalinks

Leave A Reply