Introduction :- In this article we will explore some basics of url rewriting. First we need to know what is url rewriting?. Url rewriting means modify url?s appearance. rebuild an url to a new url or make a user or search engine friendly url. url rewrite depends on mod_rewrite must be enabled on Apache server. The main purpose of url rewrite to make clean url most of sites have dynamic variables in url like:-
This is not a readable url or not useful for search engine and also not define to page purpose so using url rewrite we can get a shorter and standard url like:- http://example.com/example/xx/yy
These type of url easier to read, remember and much clean. we can also do more effort to make it more clean.
To remove .php extension like if you want change http://example.com/myfile.php to http://example.com/myfile add below code to .htaccess
if you want to give a specfic name to file like http://www.example.com/myfile.php to http://www.example.com/something
RewriteRule ? Tells Apache to applying single RewriteRule.
^something/?$ ? Regular expression or pattern. server will check this pattern on each request to redirect instead of substitution
myfile.php ? Substitution
[NC,L] ? Flags, refer Apache to apply a rule
rewrite with specific name applied for a single url rewrite for multiple or dynamic url rewriting you need to use regular expressions in pattern like :- if you using query string in url like http://www.example.com/myfile.php?id=7 and want to change http://www.example.com/myfile/7 then use
Some of meta character used in regular expressions:-
. ? any character
* ? preceding start from zero
+ ? preceding start from one
{} ? min to max quantifier
? ? ungreedy modifier
^ ? must be start
$ ? end of string
[] ? class, apply more match
? ? range if used in class
() ? group
| ? or
\ ? escape character
Some of common used url rewrite flags
Trending Tutorials