Turning dynamic URLs to static URLs
![]() | Click on a Topic [ Hide ] |
The general seo rule of thumb is that it is good practice to turn any dynamic URLs with query strings into search engine friendly static URLs. Big search engines like google love simple static URLs.
For example, if you have a dynamic URL such as
www.yourdomian.com/articles.php/?article_id=1234&article_name=apache_mod_rewrite
You can turn this URL into a static URL which looks like this.
www.yourdomain.com/article/1234-apache_mod_rewrite.html
How? Using Mod Rewrite Rule
| Learn PHP and MySQL fast! | ||
|
Learn how you can start building rich interactive database driven sites like a professional developer easily and quickly... >> Order your copy now for just $39.95 << |
|
This is possible if you are running your website under apache web server.
Apache has what's called mod_rewrite command which helps us accomplish this. All mod_rewrite command(s) sits in a file name .htaccess. This is a predefined file name which apache knows to access before processing any webpage on the browser requested by your users.
Apache mod_rewrite rule command
Here is the magic code that will convert the above dynamic example URL into a static URL.
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / rewriterule ^article/([^-]+)-([^&]+)\.html$ /articles.php?article_id=$1&article_name=$2 [L] </IfModule>
Testing mod_rewrite code example
Create a php file called articles.php. Place the following php code in it.
<?php echo "This is an article page<br>"; echo "article id:".$_GET["article_id"]; echo "<br>"; echo "article id:".$_GET["article_name"]; echo "<br>"; echo "article body: blah blah..."; ?>
Create another file called .htaccess. Place the following mod_rewrite code in it.
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / rewriterule ^article/([^-]+)-([^&]+)\.html$ /articles.php?article_id=$1&article_name=$2 [L] </IfModule>
Now, upload both articles.php and .htaccess to your web server. Upload both files in the same directory. For this tutorial, I'm going to assume you placed both the articles.php and .htaccess file in your root directory.
Now, type in www.yourdomian.com/articles.php/?article_id=1234&article_name=apache_mod_rewrite.
You should see the following output
Output
This is an article page article id: 1234 article name: apache_mod_rewrite article body: blah blah...
Now, type in www.yourdomain.com/article/1234-apache_mod_rewrite.html and should see the same page with same output.
Output
This is an article page article id: 1234 article name: apache_mod_rewrite article body: blah blah...
As you can see the new static URL ran the articles.php page where 1234 is the article_id and apache_mod_rewrite is the article name.
I hope you enjoyed this lesson. For further PHP reading and knowledge I would recommend the following...
Build large scalable dynamic websites using PHP yourself
Learn PHP like a pro >>
| Top |






