HTAccess

Problems, solutions and everything in between.
Latest entries

[SOLVED] PHP files downloading and not displaying

After upgrading one of the servers to run the latest version of PHP as opposed to a severely outdated PHP 5.3 I had a problem were Easy Apache would download the PHP files as opposed to rendering them. In order to fix this I needed to add the following lines to the .htaccess AddType application/x-httpd-php .htm AddType application/x-httpd-php .html AddHandler x-httpd-php5 .htm AddHandler x-httpd-php5 .html <FilesMatch "\.(htm|html|php)$"> SetHandler application/x-httpd-php…

Force WWW and ignore subdomain

Following on from the post where I explain how to force WWW, I've found with development domains (dev.domain.com) the htaccess is breaking the site, in the interest of keeping staging and production like for like I've added the following lines to my htaccess. RewriteCond %{HTTP_HOST} !^dev\.domain\.com$ [NC] So my Htaccess looks like the following, I have my conditions and then the rule. RewriteCond %{HTTP_HOST} !^$ RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTP_HOST}…

Redirect a .co.uk (TLD) to a .com and include all other pages

The below code will redirect all urls (both www. and non www.) to a www.domain.com RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Force files to download

AddType application/octet-stream .doc AddType application/octet-stream .pdf AddType application/octet-stream .avi AddType application/octet-stream .csv AddType application/octet-stream .xls AddType application/octet-stream .mpg

Force HTTPS

<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] </IfModule>

Force charset utf-8

To force utf-8 on an Apache server, use the following rule AddDefaultCharset utf-8

Redirect 301 with htaccess

To redirect a user, use the following command Redirect 301 /some/page.html https://www.barberriley.com/ The above rule will redirect anything who goes onto your site and visits the directory "some" and the page "page.html" it will then redirect them to the following link

Custom error pages with htaccess

To set your own custom 404 pages, direct users with the following htaccess rule ErrorDocument 400 /400.html ErrorDocument 401 /401.html ErrorDocument 403 /403.html ErrorDocument 404 /404.html ErrorDocument 405 /405.html ErrorDocument 408 /408.html ErrorDocument 414 /414.html ErrorDocument 500 /500.html ErrorDocument 502 /502.html ErrorDocument 504 /504.html

Specify the main index file for the server to look at

If a user wants to load a directory which is /foo in this case, you can determine which index file gets delivered. DirectoryIndex index.php index.html index.htm The above script will ensure the index.php file is displayed before index.html should you have the two versions in the same folder.

Force non-www to www in htaccess

Forcing either non-www to www or vice versa is crucial when it comes to SEO. Being able to access the same page with www or non-www is duplicate content when it comes to the eyes of search engines. Navigate to your root directory I'll use the below as an example /var/www/vhost/domain/htdocs/ Create a file with the name ".htaccess" - note: the dot is intentional /var/www/vhost/domain/htdocs/.htaccess Within that file, place the following text ## Force non-www urls to www RewriteEngine On…

What’s HTML character code ​? (unicode)

This would be ZERO WIDTH SPACE you can read up more on this here.