skip to Main Content

I have links like

<li><a href="search.php?domainid=5&cat=electronic">Electronic</a></li>

How can I change it to

<li><a href="electronic.html">Electronic</a></li>

I have more than 50 categories.

I am using Apache web server and PHP 5.5. Need dynamic URL Rewrite for SEO friendly URL.

<li><a href="search.php?domainid=5&cat=electronic">Electronic</a></li>

this need to be

<li><a href="electronic.html">Electronic</a></li>

and

<li><a href="search.php?domainid=13&cat=gifts">Gifts</a></li>

this need to be

<li><a href="gifts.html">Gifts</a></li>

and

<li><a href="search.php?domainid=4&cat=food">Food</a></li>

this need to be

<li><a href="food.html">Food</a></li>

and

<li><a href="search.php?domainid=11&cat=home-decore">Home Decore</a></li>

this need to be

<li><a href="home-decore.html">Home Decore</a></li>

and

<li><a href="search.php?domainid=3&cat=hotels-travels">Hotels & Travel</a></li>

this need to be

<li><a href="hotels-travels.html">Hotels & Travel</a></li>

and so on…

2

Answers


  1. A really niave example .htaccess might contain:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^.]+).html$ search.php?domainid=5&cat=$1 [L]
    

    Edit: Regarding the domainid param, you have a couple options:

    • Add more rules to translate from say %{HTTP_HOST} to whatever id the domain maps to.
    • Modify search.php to figure it out from say $_SERVER['HTTP_HOST'].

    The latter is probably the sane thing to do.

    Login or Signup to reply.
  2. Here’s the full solution

    <IfModule mod_rewrite.c>
    
      RewriteEngine on
      RewriteRule ^([a-z]*).html /search.php?domainid=5&cat=$1 [L,QSA]
    
    </IfModule>
    

    It’s only a few lines but there’s alot going on here so let’s break down what’s each little bit does

    <IfModule mod_rewrite.c>
    

    This line just opens a stanza (block) to indicate that Apache should only execute the directives inside if the specified module is loaded. In this case, the mod_rewrite module.

      RewriteEngine on
    

    Pretty simple, just turns on url-rewriting in case it already wasn’t

      RewriteRule ^/([a-z]*).html /search.php?domainid=5&cat=$1 [L,QSA]
    

    This is where all the work happens, and it’s a bit complex so I’ll break it down further. First, the anatomy of a rewrite rule

    RewriteRule Pattern Substitution [flags]

    So let’s first look at the Pattern

    ^/([a-z]+).html
    

    RewriteRule patterns are regular expressions – if you aren’t already familiar with those I’m afraid you’ll have to do some independent study as they are much to large a topic to cover here. But what I will say is that this pattern is designed to match any URI that starts at the root and has one-or-more contiguous lower-case alpha characters followed by .html. So it would match all of these

    /electronic.html
    /electronic.html?referrer=facebook
    /analog.html
    /somethingelse.html
    

    But would not match any of these

    /category/electronic.html # Because it's not root relative
    /cat5.html                # Because of the number
    /something-else.html      # Because of the hyphen
    /Electronic.html          # Because of the capital E
    

    So as you can see, regex patterns are very explicit and sensitive, so you’ll need a full understanding of the nature of your category names in order to author a proper RewriteRule pattern.

    Another thing to note in this pattern is the parentheses around the contiguous alpha characters – that creates a “captured subgroup” which can be referenced in the Substitution portion of the RewriteRule, we we need so let’s look at that next

    /search.php?domainid=5&cat=$1
    

    This tells the rewrite engine to take matched urls and internally rewrite them to according to the above pattern. See the $1? That’s the captured subgroup we got in the Pattern so it will be replaced by whatever the captured characters were.

    The last part is the [flags]

    • L simply means “Last” which tells mod_rewrite to not try to rewrite the URL again
    • QSA is “Query String Append” which will ensure that query-string data in the requested URL will survive to the rewritten one. For example, /electronic.html?referrer=facebook would be rewritten to /search.php?domainid=5&cat=electronic&referrer=facebook

    And that’s it! You’ll almost certainly need to modify this to suit your needs 100%, but I hope this is enough to get you started.

    EDIT

    Here are some alternate patterns that will match different category names

    • ^/([a-z-]+).html Allows hyphens
    • ^/([a-zA-Z]+).html Allows capital letters
    • ^/([a-z0-9]+).html Allows numbers
    • ^/([a-zA-Z0-9-]+).html Allows all of the above
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search