Htaccess Rewrite URLs Using Rewriterule , how to?

What is Rewrite URLs?

Some times you need to have a SEO URLs in your site, if your link structure is not SEO friendly like for example in WordPress default link structure www.htaccessredirected.com/?p=1 for posts or www.htaccessredirected.com/?cat=3 for categories, this links scheme is not SEO friendly because when you see www.htaccessredirected.com/?p=1 you will not know any thing about the content of this page, but suppose if you have a rewrited URL like www.htaccessredirected.com/htaccess-301-redirec/ by seeing this URL you will know that this page will talk about how to make 301 redirect using htaccess file.

How to make htaccess Rewrite URLs ?

A famous example about how we could force the redirect of the non www URLS to www URLS in a specific site, suppose we have the site name htaccessredirected.com, it will be like this:

RewriteCond %{HTTP_HOST}   !^www\.htacessredirected\.com [NC] 
RewriteCond %{HTTP_HOST}   !^$ 
RewriteRule ^/(.*)         http://www.htaccessredirected.com/$1 [L,R]

Note that to use the Rewriterule you must do the following things first:

  1. Use the Tag <IfModule mod_rewrite.c> in the beginning of the rewrite code and  </IfModule> at the end.
  2. Start bu activating the RewriteEngine by setting it to on.
  3. Determine the RewriteBase for you rewrite, for example “/”.

A full example is to rewrite URLS for WordPress Blog:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Comments
  1. No comments yet!, be the first to post a comment.

Your email will not be published, The required fields marked with '*'

*