Correctly switching between HTTP and HTTPS using .htaccess

I use something similar to this for my admin folder in wordpress: #redirect all https traffic to http, unless it is pointed at /checkout RewriteCond %{HTTPS} on RewriteCond %{REQUEST_URI} !^/checkout/?.*$ RewriteRule ^(.*)$$1 [R=301,L] The RewriteCond %{HTTPS} on portion may not work for all web servers. My webhost requires RewriteCond %{HTTP:X-Forwarded-SSL} on, for instance. If … Read more

.htaccess for cakephp

The answer is that there are 3 different .htaccess files: /var/www/app/webroot/.htaccess <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] </IfModule> /var/www/app/.htaccess <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ webroot/ [L] RewriteRule (.*) webroot/$1 [L] </IfModule> /var/www/.htaccess <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ app/webroot/ [L] RewriteRule (.*) app/webroot/$1 [L] </IfModule> It’s … Read more

Set RewriteBase to the current folder path dynamically

Here is one way one can grab the RewriteBase in an environment variable which you can then use in your other rewrite rules: RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$ RewriteRule ^(.*)$ – [E=BASE:%1] Then you can use %{ENV:BASE} in your rules to denote RewriteBase, i.e.: #redirect in-existent files/calls to index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . %{ENV:BASE}/index.php [L] Explanation: … Read more

PHP built in server and .htaccess mod rewrites

Here’s the router that I use for the builtin php webserver that serves assets from the filesystem if they exist and otherwise performs a rewrite to an index.php file. Run using: php -S localhost:8080 router.php router.php: <?php chdir(__DIR__); $filePath = realpath(ltrim($_SERVER[“REQUEST_URI”], “”)); if ($filePath && is_dir($filePath)){ // attempt to find an index file foreach ([‘index.php’, … Read more

CSS, JS and images do not display with pretty url

When you use relative url’s, the browser will dynamically create a complete url by using the url of the resource it loaded. In other words: It uses the url as it is displayed in the address bar. In your case ( it tries to load any relative url relative to Your resources are however … Read more

How to debug htaccess rewrite script

Note to readers: the old answer doesn’t work anymore. As of version 2.4, Apache no longer allows the RewriteLogLevel and RewriteLog directives. Now they’re all bundled with the single LogLevel directive (see Log Files documentation), which supports module-specific log levels with prefixes and trace[1-8] constants. To set the highest level of logging specifically for the … Read more