Forum Moderators: phranque
# Dont list files or folders
Options -Indexes
#
# Dont show server details
ServerSignature Off
#
RewriteEngine On
RewriteBase /
#
# Add WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) http://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# add trailing slash if missing
rewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [R=301,L]
#
# Allow Access to real files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#
# Everything else goes to loader
RewriteRule ^(.*)$ loader.php?t=$1 [L,QSA]
#
# CACHE EVERYTHING...
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# Html
ExpiresByType text/html "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
# Music
ExpiresByType audio/mp3 "access plus 1 year"
</IfModule> # Add WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) http://www\.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# add trailing slash if missing
rewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [R=301,L]
#
# Allow Access to real files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#
# Everything else goes to loader
RewriteRule ^(.*)$ loader.php?t=$1 [L,QSA]
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ http://www.example.com/$1/ [R=301,L]
It may or may not be preferable to express the pattern as ^([^.]+[^./])$to simplify the capture. RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
No point in saying REQUEST_URI if you've already captured it anyway. Periods in the target don't need to be escaped. Nothing will break; it just isn't needed. (And speaking of not needed: You don't need a RewriteBase. It's safer to put a / at the front of each Rewrite target. This is only relevant for internal rewrites; external redirects look the same either way.) RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ /loader.php?t=$1 [L,QSA]This should be immediately preceded by RewriteRule ^loader\.php - [L]Since you already know this file physically exists, and there will be a lot of internal requests for it, get it out of the way as soon as possible. Options -Indexes
ServerSignature Off
RewriteEngine On
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ http://www.example.com/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ /loader.php?t=$1 [L,QSA]
RewriteRule ^loader\.php - [L]
# Custom 404 - just in case someone tries to go to file that doesn't exist - example.com/notreallythere.png
ErrorDocument 404 /404.php if a user does try to access a file that doesn't exist - can I include a link to custom 404 page - or will it just be picked up by the loader.php - and the 404 will be shown through the template engine?Under your new CMS, all user requests will be for files that don't exist. That's the point of your loader.php. And paradoxically that's why you may not need the -f test: you already know it will always test positive, assuming the body of the rule has a pattern that will only match pages, not supporting files, and that you've previously excluded requests for loader.php. (General guideline: never put something in a RewriteCond that can go in the pattern of a RewriteRule. That's a guideline, not a law.)
$page = array('title' => 'My Page 1','heading' => 'Welcome!');
<title><?php echo $page['title']; ?></title>
$url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
// All the code to get just the part I need - which becomes $filename
// Is page cached:
if(file_exists('cache/' . $fileName . '.txt')){
include_once('cache/' . $fileName . '.txt');
die();
}
//If not.. build page...
include('pages/' . $filename . '.php');
ob_start();
include "template.php";
$contents = ob_get_contents();
ob_end_clean();
$fp = fopen(('cache/' . $fileName . '.txt'), 'w');
fwrite($fp, $contents);
fclose($fp);
echo $contents;
<?php
$url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
// All the code to get just the part I need - which becomes $filename
// Is page cached:
if(apcu_exists($fileName)){
echo apcu_fetch($fileName);
die();
}
//If not.. build page...
include('pages/' . $filename . '.php');
ob_start();
include "template.php";
$contents = ob_get_contents();
ob_end_clean();
apcu_store($fileName,$contents);
echo $contents;
?>
include('pages/' . $filename . '.php');What happens if filename.php doesn't exist? Seems like you should start your buffering earlier, so you can detour to the 404 track if necessary. Otherwise bad requests will lead to empty pages--or pages that contain nothing but what's in the template.
if(!file_exists('pages/' . $fileName . '/index.php')){
header("HTTP/1.0 404 Not Found");
include_once("404.php");
die();
}else{
// The load page part show above
}
<?php
$url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
// All the code to get just the part I need - which becomes $filename
if(!file_exists('pages/' . $fileName . '/index.php')){
header("HTTP/1.0 404 Not Found");
include_once("404.php");
die();
}else{
//If not.. build page...
include('pages/' . $filename . '.php');
ob_start();
include "template.php";
$contents = ob_get_contents();
ob_end_clean();
echo $contents;
}
?>
Options -Indexes
ServerSignature Off
RewriteEngine On
RewriteRule ^(files)($|/) - [L]
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ http://www.example.com/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ /loader.php?t=$1 [L,QSA]
RewriteRule ^loader\.php - [L]
RewriteRule ^(files)($|/) - [L]This can be simplified to
RewriteRule ^files - [L]No capture, no closing anchor. That is, I assume you don't concurrently have a directory called, say, /filesand/ or similar, which is not to be protected. If you want to protect yourself against this possibility, you can always put a word boundary: RewriteRule ^files\b - [L]Unless you've got a directory called /files-and-more/ (a hyphen counts as a word boundary) in which case I wash my hands of you ;)
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-f There is one problem - When I removed this line as most files are REAL:
RewriteCond %{REQUEST_FILENAME} !-f
If they go to example.com/my-page/index.php <-- This fails as the file doesn't exit
I don't really want them going to the index.php page - but I don't know if search engines want to try and access the .php file (even though it doesn't exist)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
The index.php files are no longer real
RewriteCond %{REQUEST_URI} ^/((?:\w+/)*)index\.\w+
RewriteRule index\.(?:html|php)$ https://www.example.com/%1 [R=301,NS,L]Ordinarily this will be your second-to-last redirect, immediately before the domain-name-canonicalization rule (the one that handles www and/or https). Replace \w with [\w-] if you use hyphens in URLs. If the index.php files never, ever really exist, I guess you don't need the [NS] flag, but it does no harm.
Options -Indexes
ServerSignature Off
RewriteEngine On
RewriteRule ^files - [L]
RewriteRule ^loader\.php - [L]
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ http://www.example.com/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/((?:\w+/)*)index\.[\w-]
RewriteRule index\.(?:php)$ http://www.example.com/%1 [R=301,NS,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ /loader.php?t=$1 [L,QSA]
That unfortunately made it so my visitors HAD to have index.php at the end of the urls - unless I put it in the wrong place!
All my site links are just example.com/directory/ and that's how id like the visitors still to use them. Being able to access the index.php was just for search engines.
So example.com/directory/ and example.com/directory/index.php both needed to be routed through my /loader.php?t=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/((?:\w+/)*)index\.\w
RewriteRule index\.(?:php)$ http://www.example.com/%1 [R=301,NS,L]These two rules are in the wrong order. First the index redirect, then the domain name. So it would be RewriteCond %{REQUEST_URI} ^/((?:\w+/)*)index\.\w
RewriteRule index\.(?:php|html)$ http://www.example.com/%1 [R=301,NS,L]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]When I said to replace \w with [\w-] I should have specified that it only applies to the rest of the URL, because obiously you don't have extensions beginning in - hyphen. but it's stopping the loader.php get pages to the site rootOh, oops, you'll need to change
Options -Indexes
ServerSignature Off
RewriteEngine On
RewriteRule ^files - [L]
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ http://www.example.com/$1/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/((?:\w+/)*)index\.\w
RewriteRule index\.(?:php|html)$ http://www.example.com/%1 [R=301,NS,L]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]*)$ /loader.php?t=$1 [L,QSA]
RewriteRule ^loader\.php - [L] Options -IndexesThis line tells the server not to generate an automatic index for directories that do not contain an index.html file (or index.php, or whatever else you have specified in your DirectoryIndex line). It has nothing to do with allowing access to index files that physically exist. Users who request the bare directory will get a 403 response if and only if there is no index.html (index.php) in the directory and you don't have a rewrite such as loader.php that deals with the request in some other way. RewriteRule ^loader\.php - [L] RewriteRule ^(files|loader\.php) - [L]But keep an eye on your logs to make sure you never get an explicit request for /loader.php. If you do, you'll need a rule for it. This is one of the many situations where you don't need to deal with an issue unless and until it actually arises.