Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)

To start with your favorite solution: <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L] </IfModule> In the part handling non-https URLs you are redirecting to %{HTTP_HOST}. Then, in case your host name started with “www”, a second redirect has to take place to send … Read more

Allow/deny image hotlinking with .htaccess

RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC] RewriteCond %{HTTP_REFERER} !^http://(www\.)?otherdomain\.com [NC] RewriteRule \.(gif|jpe?g|js|css)$ – [F,NC,L] Will work, as this says. “Refererr is not nothing, and referer is not matching mydomain and referer is not matching otherdomain. If it were the case that you were trying to do the opposite (blacklist a set of domains from … Read more

.htaccess to redirect images

I am not sure this will solve the problem, but instead of modifying all the links you could use the BASE element in each page to define a base URI: From http://www.w3.org/TR/html4/struct/links.html#h-12.4: When present, the BASE element must appear in the HEAD section of an HTML document, before any element that refers to an external … Read more

PHP Rewrite Rules

Create a file called .htaccess in the root of your website and put this in it. RewriteEngine on Options +FollowSymlinks RewriteBase / RewriteRule ^(.*) search.php?search=$1 [R] Should do the trick. I would suggest however that you make it a bit more specific, so maybe require the user of a search directory in your url. eg … Read more

htaccess subdomain

This is a wildcard based solution, so it sould work for any number of subdomains. This will redirect domain.com/foo/bar to foo.domain.com/bar: RewriteCond %{HTTP_HOST} ^domain.com$ [NC] RewriteRule ([^/]+)(/.*|$) $1.domain.com/$2 [R=302] This will handle (internal rewrite) the virtual hosts: RewriteCond %{HTTP_HOST} ^(.*).domain.com$ [NC] RewriteRule (.*) %1/$1 [L] You might consider using 301 (permanent redirect) instead of the … Read more

What is L in [QSA, L] in htaccess

.htaccess flag list C (chained with next rule) CO=cookie (set specified cookie) E=var:value (set environment variable var to value) F (forbidden – sends a 403 header to the user) G (gone – no longer exists) H=handler (set handler) L (last – stop processing rules) Last rule: instructs the server to stop rewriting after the preceding … Read more