Url manipulation with mod_rewrite and php-catcher for beginners
Url manipulation is one of the hottest areas on the new generation web (2.0). It provides more security and flexibility. It is user and search-engine friendly. The most popular combination is Apache-webserver + mod_rewrite + server-side catcher(mostly a php-file). In this tutorial we use very short examples to make it easy for newbies.
Why url manipulation & mod_rewrite?
- more security: hide file location
- shorter urls without file extensions like .html
- easy to remember / recommend urls
- search engine friendly
- easy to manage structure
Example:
old url:
example.com/some-path/and-file.php?long=string&with=bloody&session_id=a5kg7hirh39573...
new url:
example.com/2009/05/25/article-about-history
Can you tell a friend the first url on the phone or send with a sms?
Files:
- .htaccess
- catch.php
- page1.html
- page2.html
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ catch.php?p=$1 [L,QSA]
"RewriteCond %{REQUEST_FILENAME} !-f" means if requested path is not an existed file
"RewriteCond %{REQUEST_FILENAME} !-d" means if requested path is not a real directory
catch.php
<?php
$p = $_GET[p]; // p means page or path
// use white-lists against possible code-injections
$my_pages["my/first/url"] = "page1.html";
$my_pages["second-url"] = "page2.html";
if( $my_pages[$p] && file_exists( $my_pages[ $p ] ) ){
include( $my_pages[ $p ] );
} else {
echo "Page not found: $p";
}
page1.html
Welcome to page 1!
page2.html
This is page 2.
Now test it with calling urls on your website:
yoursite.com/my/first/url
yoursite.com/second-url
yoursite.com/non/existing/path
Links for detailed information & guides:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
http://en.wikipedia.org/wiki/Rewrite_engine
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModRewrite
http://msdn.microsoft.com/en-us/library/ms972974.aspx
http://www.sitepoint.com/article/guide-url-rewriting/
| Attachment | Size |
|---|---|
| Download url_manipulation_mod_rewrite.zip | 1.06 KB |
Similar entries
- .htaccess examples
- Prevent hotlinking with htaccess and mod_rewrite
- Redirect webpages with HTML, PHP, .htaccess, Java+Script, CGI-Perl, ASP.NET and ColdFusion
- Include both php/ html files, catch & assign output to a string variable
- Search in text files recursively with PHP - Grep
- Web Application Security Papers
- How to setup auto_prepend_file with safe mode and open basedir restriction
- Howto protect admin.php / login.php with htaccess password
- need help for customizing PHP-grep files search
- Next generation URLs, hostname/id without www and long-keywords
- Page generation time and http-referers with PHP
- Bing vs. Google: a small comparison
- Get Full Url Path excluding PHP Script's name
- Set auto_prepend_file with .htaccess on Hosteurope managed hosting
- Iframe Trojan / Virus of Alcobro.net
- Redirect a query to multiple search engines with one form and javascript
- A simple website structure
- Fix Google Adsense Search and Drupal Conflict
- Drupal 6 vs. Joomla 1.5 (2009)

Comments
SEF Ulrs
Making the Urls Search Engine Friendly by keyword rich in the url.It is liked by search engines and it is easy to remember by humans also.Your url will rank high if it is seo friendly.
Post new comment