User Friendly Url by htaccess | htaccess URL Re-write Example


1.
http://www.vijaylathiya.wordpress.com/products.php?category=flower&colour=red
to
http://www.vijaylathiya.wordpress.com/products-flower_red.html

RewriteEngine on
RewriteBase /
RewriteRule ^products-([^/\.]+)_([^/\.]+).html?$ products.php?category=$1&colour=$2 [L]

2. Create Custom error page and redirect user on created custom error page while 401,403,404,500 error occurs. This is very useful if you want users to not feel lost when they reach the dreaded 404 Not Found, which is a plain white page with information about Apache and your OS.

Add Code in .htaccess file..

ErrorDocument 401 /index.html
ErrorDocument 403 /index.html
ErrorDocument 404 /index.html
ErrorDocument 500 /index.html

3.
http://www.vijaylathiya.wordpress.com/user.php?name=vijay
to
http://www.vijaylathiya.wordpress.com/vijay

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_.@]*)$ user.php?name=$1 [L]

4
Add WWW prefix

http://example.com
to
http://www.example.com

#add WWW prefix
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

5
Remove WWW prefix
http://www.example.com
to
http://example.com

#Remove WWW from Url
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

6
Redirect index.php to Root

RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]

7
Set Custom 404 not found page
ErrorDocument 404 http://www.example.com/404.php

8
Prevent Robots from Indexing Sitemap XML, Php file

<Files ~ "\.xml$">
Header set X-Robots-Tag "noindex"
</Files>

<Files ~ "^(sitemap|sitemaptag)\.php$">
Header set X-Robots-Tag "noindex"
</Files>

9 Set Cookie, Cache for specific time period

<filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|ogg|mp3)$">
Header set Cache-Control "max-age=290304000, public"
</filesMatch>

<FilesMatch "!\.(gif|jpe?g|png|css|js)$">
    php_value session.cookie_domain domainname.com
</FilesMatch>

FileETag MTime Size
<ifmodule mod_expires.c>
  <FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresActive on
ExpiresDefault "access plus  1 year"
  </FilesMatch>
</ifmodule>
<FilesMatch "\.(txt|xml|js|css)$"> Header set Cache-Control "max-age=1209600"
10 # Enable GZIP

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript


11 # Disable Directory Listing
# Disable Directory Listings 
Options -Indexes
12 # Enable cross-site use of font-files
<FilesMatch "\.(svg|xml|ttf|otf|eot|woff)$">

Header set Access-Control-Allow-Origin "*"


13 # Accept Encoding .htaccess rule

   <FilesMatch "\.(js|css|xml|gz)$">
     Header append Vary: Accept-Encoding
   

14 Force url to lower case if uppercase character found in Url - Add below rule in .htaccess file

# Check Uppercase character in Url
RewriteCond %{REQUEST_URI} [A-Z]

# ensure it is not a file on the drive first
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule (.*) rewrite-strtolower.php?rewrite-strtolower-url=$1 [QSA,L]

- create above name Php file with below convert script to convert uppercase character to lower

15 Redirecting a Web Folder Directory to another Directory

#Using htaccess redirect one page to another :
RewriteRule ^url-string-to-redirect$ http://www.yourdomain.com/your-new-url-string [R=301,L]

or 

Redirect 301 /path/to-old-url    http://www.cyourdomain.com/path/to-new-url

#To redirect the contents of a whole directory to another use the below:
RewriteRule ^subdirectory/(.*)$ /anotherdirectory/$1 [R=301,NC,L]

To use above htaccess rewrite rule Make sure that the starting of the .htaccess file contains the 2 lines of code below which enables the Apache to rewrite the URLS, then place your redirections rule below them Options +FollowSymLinks RewriteEngine On
16 - Call jpeg, png Images & JS file from cookieless Domain

Unset cookie for images, js, css etc file using htaccess rule


    
  Header unset Set-Cookie
 

Importance of Images, CSS, js etc static content file call from cookie less domain

Static content calling from Cookieless domain is a best practice for achieve standard speed performance in Website. instead of adding above cookie unset htaccess rule you can also achieve good speed by setup cookieless sub domain, or use other domain with making it cookieless and call static content from it. even you can also use cookieless Cloud Network MaxCDN and CloudFlare ( For MaxCDN Vs CloudFlare )
More information on Benefits of Serving Static Content from a Cookieless Domain read Yahoo Developer's Speed performance article Use Cookie-free Domains for Components section

17 - Redirect folder all url to root domain

To redirect folders all url to main root domain set below .htaccess redirection code inside folder .htaccess file


RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ / [R=301,L]




Post a Comment

0 Comments