Stefany93 Posted September 29, 2013 Share Posted September 29, 2013 Note: This article assumes you have a basic understanding as to what .htaccess filesare, how they work and how to create one. And that you know how to use regular expressions.When we were green programmers, we would create a website with these files: index.php about.php contact.php And when our visitors wanted to access a certain web page within our website, we wouldprovide them with links to these documents directly like this: www.example.com/about.php However, all this above turned out to be the wrong way for many reasons. First of all,referencing web pages with their full name is not very secure because an evil user would know(a where "about.php" is located, b) that it is a PHP file and that will facilitate his work to ruin our website.The correct way to fix this is with a .htaccess file placed within out root directory where our website resides.Before you use your .htaccess file to re-write an URL, make sure your server a) supports .htaccess files because somebosses of hostings forbid them for God knows what reasons and b) that even if the hosting allows .htaccess files, you mightneed to contact the hosting people to ask them to configure their server to accept URL re-write on a per directory basis.Now we would want to turn this URL http://www.example.com/about.php into http://www.example.com/aboutOur very first directive must be: RewriteEngine On # We are turning on the rewrite engine on our server Some hostings require you to specify as to from where the re-writing shall begin.Let's say you store all of your website files and folders in your root directory.Then your second directive will be: RewriteBase / # We shall be re-writing files in our root directory. When testing on localhost using the "RewriteBase" directive will give a server error.Our third directive: RewriteRule about about.php Now, golden rule - when re-writing a URL, you must first specify the RewriteRule directive and thenthe word right after that is the word that rewrites the name of the file whose name you wantto re-write. So RewriteRule about means that the word that will come after "about" is the onethat will henceforth be known as "about" in this case about.phpSo: RewriteRule about about.php # Re-write the file name about.php to about So when we write in the URL http://www.example.com/about the server will actually be pulling the informationfrom about.php but the user will never know! That will be our little secret!Now the problem with the above approach is that the re-writed name is not spesific. Meanning that "about"will match any URL that has the word "about" in, for example: http://www.example.com/aboutmyawesomepizza The above will be matched. We do not want that. We can solve this problem with regular expressions: RewriteRule ^about/?$ about.php Super! Now our URL will only match http://www.example.com/about with or without trailing slash, does not matter.One last thing - we want about URL to be case-insensitive so that http://www.example.com/about and http://www.example.com/AboUt will bothmatch the about.php file. We do that by putting the [NC] flag at the end of the directive like so: RewriteRule ^about/?$ about.php [NC] "NC" meanning "no case". I also thought it meant "North Carolina" but sadly it did not.All directives put together: RewriteEngine On RewriteBase / RewriteRule ^about/?$ about.php [NC] More reading:What are .htaccess files? - http://httpd.apache.org/docs/2.2/howto/htaccess.htmlRewriteRule directive - http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule Quote Link to comment https://forums.phpfreaks.com/topic/282532-super-simple-url-rewriting-tutorial-with-htaccess/ Share on other sites More sharing options...
Ch0cu3r Posted September 29, 2013 Share Posted September 29, 2013 (edited) Your post is informative with basic usage of mod_rewrite however this should of been posted in Apache mod_rewrite forum and not here. This is help with PHP code. Do the note mapping files without file extensions can also be achieved by adding MultiViews to Options flag in .htaccess Options +MultiViews Edited September 29, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282532-super-simple-url-rewriting-tutorial-with-htaccess/#findComment-1451704 Share on other sites More sharing options...
Stefany93 Posted September 29, 2013 Author Share Posted September 29, 2013 Your post is informative with basic usage of mod_rewrite however this should of been posted in Apache mod_rewrite forum and not here. This is help with PHP code. Do the note mapping files without file extensions can also be achieved by adding MultiViews to Options flag in .htaccess Options +MultiViews Sorry, I was searching for the correct category and couldn't find one If a moderator is so kind as to move the topic that will be great. Thanks for the update. Quote Link to comment https://forums.phpfreaks.com/topic/282532-super-simple-url-rewriting-tutorial-with-htaccess/#findComment-1451706 Share on other sites More sharing options...
Ch0cu3r Posted September 29, 2013 Share Posted September 29, 2013 (edited) Did you look up and down the forum index about half way Edited September 29, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282532-super-simple-url-rewriting-tutorial-with-htaccess/#findComment-1451709 Share on other sites More sharing options...
Stefany93 Posted September 29, 2013 Author Share Posted September 29, 2013 Did you look up and down the forum index about half way Well it is too late to move it now by myself. Quote Link to comment https://forums.phpfreaks.com/topic/282532-super-simple-url-rewriting-tutorial-with-htaccess/#findComment-1451710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.