We can rewrite urls using .htaccess files. But before writing something in .htaccess files we need to verify which kind of server we are using. Here in the case you may use either an apache server or a Nginx server. You can easily check by just adding a
<?php phpinfo(); ?>
in a php page.
Case 1 : Apache Server:
Ensure that mod_rewrite module enabled in apache server. This can be identified by checking phpinfo included page.
Common methods used for url rewriting is as follows
RewriteEngine
This is mandatory for any apache server. This is used to enable the rewriting engine. In some cases it should be on by default.So ensure that following code must be mandatory in top of your htaccess file
RewriteEngine On
RewriteRule
in some cases you may have only few pages and you can specify each page in your website in .htaccess file. In such case you can use these kind of rewriting
For example
RewriteRule ^article/used/to/be/here.php$ /article/now/lives/here/ [R=301,L]
Suppose you have to rewrite url like these
http://en.wikipedia.org/wiki/url_rewriting
You should write something like these on your .htaccess file
RewriteEngine On
RewriteRule ^wiki/(.+)$ w/index.php?title=$1 [L]
But actual implementation of the page will be like these
http://en.wikipedia.org/w/index.php?title=url_rewriting
RewriteCond
This can be used for rewriting certain url with respect to some conditions. Suppose you need to add or remove www with your website name and on certain condition redirect to certain page like "url not available or error message "
Example :
RewriteCond %{HTTP_REFERER} !^http://(www.)?example.com/.*$ [NC]
You can further refer here
Introduction to url rewriting
Rewrite with examples
Case 2 : nginx Server
Enable module HttpRewriteModule on your nginx server
Use location to rewrite you urls as per nginx settings
Example
location / {
root /path/to/drupal;
index index.php index.html;
try_files $uri $uri/ @rewrite;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}