force WWW to the URL name via .htaccess & more tips

Let us talk about scripts, HTML, Perl, PHP, apache, etc.

force WWW to the URL name via .htaccess & more tips

Postby Stevyn » Thu Feb 21, 2013 5:57 pm

Code: Select all
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$$1 [R=301,L]
Contact me directly: Ironfeatherbooks (@)

User avatar
Posts: 1565
Joined: Sun Nov 08, 2009 7:03 pm
Location: Colorado

Re: force WWW to the URL name via .htaccess

Postby Stevyn » Sun Jul 14, 2013 3:07 pm

how to take off the WWW

Code: Select all

 RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

# remove www from host
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]

Contact me directly: Ironfeatherbooks (@)

User avatar
Posts: 1565
Joined: Sun Nov 08, 2009 7:03 pm
Location: Colorado

Re: force WWW to the URL name via .htaccess

Postby Stevyn » Sat Mar 21, 2015 8:16 pm

Useful .htaccess ... ollection/

15 .htaccess Snippets to Make Your Site Much More Useful

SEO-friendly URLs

Websites with clear URL structure rank higher than those with addresses like ‘index.php?product_id=’. Ideally, a SEO-friendly URL should feature a keyword and duplicate some content from your blog post title or the name of the page, which guarantees it will be properly indexed by Google or other search engines.


<Files magic>
ForceType application/x-httpd-php5

Redirect form your old domain to the new one

The technique is better known as 301 redirect. With its help you can redirect both separate pages and the entire sites. To redirect a single page, use code:

Redirect 301 /oldpage.html

For the entire site:

Redirect 301 /

In both cases, the old URL comes first, with the address of the new domain following it in the second part of code.

Remove www from URL

For the users’ convenience or for better SEO ranking, you might want to remove www from the URL of your site. With the help of the following code you’ll be able to remove www and forward the users to your site address starting with http:/ ….

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

Error Pages

None of your visitors should see a blank page when they end up on a broken URL. Instead, create a beautifully-designed and informative error page that would provide the visitors with working links to keep on browsing your site.

ErrorDocument 400 /errors/badrequest.html
ErrorDocument 401 /errors/authrequest.html
ErrorDocument 403 /errors/forbidden.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/servererror.html

Better site speed with better caching

The faster your page loads, the higher your site will rank in search results. Web developers make a struggle to create websites that would smoothly run on both desktop and mobile devices, and not make them wait for long till the site loads. That’s when caching comes in handy. However, at this point there is one important thing to consider – you should make sure there are no other caching systems in place. Additionally, you need to decide on caching length. In the example below you can see how to set files to cache for 24 hours.

<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
Header set Cache-Control "max-age=28800"

Prevent files from caching

If you don’t want particular files to cache, you can easily control it by adding a certain code to the .htaccess file. Here is how to disable caching for the particular files:

<FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control

Site on maintenance note

I guess each of us has once come across Apple’s popular “Coming Soon” maintenance page that appears when they take their store down, before a new product launch. You can follow this example and introduce our visitors to such a page before some big announcements. With the help of the following code you can redirect everyone except yours’ IP address and that stands for a special “home” IP address.

RewriteCond %{REMOTE_ADDR} !your_ip_address
RewriteCond %{REMOTE_ADDR} !
RewriteRule !offline.php$ [L,R=307]

Use the vary header to crawl mobile content

Many web developers give the same URL for both mobile and desktop versions of their websites. Instead of opting for the responsive design, these make alter the HTML. In such cases, the “Vary” header is an indispensable component to make Google know that the HTML changes for mobile web users, which provides for better page indexing.

Header append Vary: User-Agent

Prevent image hotlinking

As so often happens, someone steels images from your website without your permission. That’s what people call image hotlinking. By steeling images from your site, people also take advantage of your bandwidths. For this not to happen, add the following code into your .htaccess.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ [NC,R,L]

rel=”canonical” tag

In case you have several pages with similar content or you want prevent search engines from indexing other websites that have copied your content, you can make user of rel=”canonical” tag to make Google know that your page is the original one. Also, the same tag can be applied to images and PDF files. Here is how the latter works.

<Files download.pdf>
Header add Link '<>; rel="canonical"'

<Files product.jpg>
Header add Link '<>; rel="canonical"'

Block access to backup and source files

Some file may go under risk and pose a great security danger when someone has access to them. For it not to happen, apply the following code.

<FilesMatch "(\.(bak|config|dist|fla|inc|ini|log|psd|sh|sql|swp)|~)$">
## Apache 2.2
Order allow,deny
Deny from all
Satisfy All

## Apache 2.4
# Require all denied

Password protect a directory

Protecting documents, images and other data from unauthorized users is of high value. Of course, you can accomplish this by means of PHP to ask users for login authorization information, however the same can be done much easier and effectively with .htaccess. You will need to prepare two files – the first one is the .htaccess file with code and another one is .htpasswd file with usernames and passwords off all the allowed users. Here is how the .htaccess file looks like.

AuthType Basic
AuthName "restricted area"
AuthUserFile /home/davidwalsh/html/protect-me-dir/.htpasswd
require valid-user

Force files to download

When you provide some content on your website, you can share a link for the users to open a document, audio or video file, etc. in a new window or download it to their desktops. To achieve the latter, add the following code to the .htaccess file and your visitors will be provided with a copy of the content you’ve share onsite.

<Files *.md>
ForceType application/octet-stream
Header set Content-Disposition attachment

Gzip compression

Compression of HTML and CSS files is quite popular now as it provides for faster page loading. If for some reason you still don’t compress files on your site, it’s high time to start practicing it. Add this code to the .htaccess file on your server.

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

Ban someone from your site

If you don’t want particular users or some malicious parties to have access to your site content, then you can easily ban them from your website with the help of the following code:

<Limit GET POST>
order allow,deny
deny from 123.456.78.9
deny from 987.654.32.1
allow from all
Contact me directly: Ironfeatherbooks (@)

User avatar
Posts: 1565
Joined: Sun Nov 08, 2009 7:03 pm
Location: Colorado


Who is online

Users browsing this forum: No registered users and 1 guest