Forum Moderators: phranque
<?php header('Content-type: text/html; charset=utf-8');
if ( $_SERVER['HTTPS'] )
{
$host = $_SERVER['HTTP_HOST'];
$request_uri = $_SERVER['REQUEST_URI'];
$good_url = "http://" . $host . $request_uri;
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: $good_url" );
exit;
}
?>
AddType application/x-httpd-php5 .htm .html
RewriteEngine On
# REDIRECT htm INDEX PAGES to index/
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ https://www.example.com/$1 [R=301,L]
# Get rid of extra path info such as example.com/pagina1.htm/maps/ etc
RewriteRule ^((?:[^./]+/)*[^./]+\.(?:html?|php))/ http://www.example.com/$1 [R=301,L]
# Redirect non-canonical to www
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} !^(www\.example.com\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_HOST} !^(www\.example.com\.com)?$
RewriteRule (.*) https://www.example.com/$1 [R=301,L] [edited by: coopster at 11:01 pm (utc) on Apr 8, 2013]
[edit reason] example.com was misspelled; fixed it [/edit]
htaccess isn't cached, because the browser doesn't download it. Only the page itself can be cached. The browser only "sees" htaccess when it makes a request.
The problem is that Redirect Responses are cacheable. Once the browser knows a URL redirects, it does it automatically without checking the redirect is still in place on the server. That has confused me on many occasions.