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
A really niave example
.htaccess
might contain:Edit: Regarding the
domainid
param, you have a couple options:%{HTTP_HOST}
to whatever id the domain maps to.search.php
to figure it out from say$_SERVER['HTTP_HOST']
.The latter is probably the sane thing to do.
Here’s the full solution
It’s only a few lines but there’s alot going on here so let’s break down what’s each little bit does
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.
Pretty simple, just turns on url-rewriting in case it already wasn’t
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
So let’s first look at the Pattern
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 theseBut would not match any of these
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
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 againQSA
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