Posts Tagged ‘.htaccess’
Redirect Only Specific Files from HTTP to HTTPS
Sunday, January 27th, 2008
I had a hard time finding a solution to redirect only certain files from HTTP to the secure HTTPS. I found all kinds of ways to redirect the entire site to HTTPS but none for specific pages only. After a few hours tinkering I found a solution using the following code in a .htaccess file:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
rewritecond %{SERVER_PORT} !^443$
rewriterule ^filename\.php(.*)$ https://www.example.com/filename.php$1 [r=301]
The first part of the code redirects example.com to www.example.com as noted in an earlier post, “301″ Redirects for SEO. The second part then redirects a specific file (filename.php) from http to https. Thus the individual file “filename.php” can only be accesses using a SSL connection.
Posted in Website Design. ⋅ Tags: .htaccess, http, https, redirects, SSL
“301″ Redirects for SEO
Friday, January 4th, 2008
Search engines will often regard www.example.com and example.com as two different websites. Because of this, websites usually experience the effects of link fragmentation (inbound links point to both www.example.com and others to example.com). For example, if www.example.com has 5,000 inbound links and example.com has 2,500 inbound links you could potentially combine them so you have all 7,500 links pointing to www.example.com.
To do this you can setup a permanent “301″ redirect by creating a .htaccess file with the below code. This will ensure that all requests coming in to example.com will get redirected to www.example.com.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
Notes:
- The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)
- REPLACE example.com and www.example.com with your actual domain name in the code above
- This .htaccess redirection method only works on servers using the Apache Mod-Rewrite module
This “301″ redirect method is also a great way to save money on SSL certificates. When the site always points to www.example.com you only need to buy a single SSL certificate.
Posted in Website Design. ⋅ Tags: .htaccess, redirects, SEO, SSL
Using PHP on Pages with HTML Extensions
Friday, January 13th, 2006
Recently I wanted to use PHP includes in an existing site so I could make updates to common parts of the site faster by editing one file (e.g.: header, navigation, footer, etc.). The problem was the site was designed with all pages ending in .html. Of coarse I could have changed all the pages to .php but the site has very good search engine rankings, and many other sites already link to these .html pages. After a little research, I found the solution to be the .htaccess file.
Since pages without a .php-extension will not be parsed by PHP by default on most web servers, you need to tell it to treat all HTML pages as PHP pages. You do this by simply adding a line to the .htaccess file located in your site’s root directory (or create a .htaccess file using a text editor). Just add:
AddType application/x-httpd-php .php .html .htm
Note: .htaccess files need to be placed in each directory that will make use of PHP in .html pages.
Update: If you get an 500 Internal Server Error try:
AddHandler application/x-httpd-php .php .html .htm
