Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Firstly I'm still beginner in mod_rewrite.
Everybody if I have this .htaccess code

RewriteEngine On
RewriteRule ^index/(.*)/$ index.php?usr=$1 [L]

Is must change the form of the link(href) to


<a href="index/image/">Click Here</a>

Or leaving it as is


<a href="index.php?usr=5">Click Here</a>

If leave it as is <a href="index.php?usr=5">Click Here</a> Will show the link this way


Here's a sample of the html code.

<div id="main_menu"> 
    <li><a href="index.php">{$lang.Nav_Home}</a></li> 
    <li><a href="sections.php?cid={$value.cat_id}">{$value.cat_name}</a></li> {else} 
    <li id="moreSection_{$secNum}" style="display: none;"><a href="sections.php?cid={$value.cat_id}">{$value.cat_name}</a></li> 
    <li><a href="javascript:;" onclick="showHideMoreSec('moreSection',this)"> <img src="images/arrow_down.png" alt="{$lang.MainMenu_MoreSection}" title="{$lang.MainMenu_MoreSection}" /> </a></li> 
share|improve this question
@slm: what is the right way to add hyperlink(href) after using .htaccess code <a href="index/image/">Click Here</a> or leave it as <a href="index.php?usr=5">Click Here</a> ? – Lion King Dec 31 '12 at 23:38
With this way I must change every hyperlink in the script to the new friendly url and this is very difficult. the problem is how can I change all hyperlinks that in the script at once to new friendly url ? – Lion King Dec 31 '12 at 23:51
No, I want to change this <a href="sections.php?cid={$value.cat_id}">{$value.cat_name}</a> Specifically sections.php?cid={$value.cat_id}, because when click on this link will show as old style url, and I would like to show as friendly url. – Lion King Jan 1 '13 at 0:48

I think your issue is that you have to deal with the PATH and QUERY_STRING as 2 separate elements.

                   ^^^^^^^^^^^^ ^^^^^
                   ||||         ||||
                   path         query string

Try a set of rules like so:

RewriteEngine on
RewriteCond %{QUERY_STRING}     ^$            [OR]
RewriteCond %{QUERY_STRING}     ^cid=(.*)$    [NC]
RewriteRule ^/sections.php$     /sections/%1? [NC,L,R=301]

These rules are doing the following. 1st RewriteCond is skipping if there isn't a QUERY_STRING. The 2nd RewriteCond saves the bit after the equal sign in a variable, %1. The last line, RewriteRule, builds up our new PATH, /sections/%1, any time a /sections.php line is encountered, and had a QUERY_STRING, cid=....

A sample html file:

  <a href="/sections.php?cid=5">Click Here</a>

Hovering on the Click Here link on the above page shows this:


Clicking this link takes me to this URL:


my /var/www/html has the following:

% tree /var/www/html
|-- test.html
`-- sections
    `-- 5

1 directory, 2 files


share|improve this answer
unfortunately, when I click on the link http://localhost/sections.php?cid=5 not take me to http://localhost/sections/5, because address bar displays the link after click as . – Lion King Jan 1 '13 at 2:05
my script inside folder ebdaa_v3 – Lion King Jan 1 '13 at 2:11
localhost isn't a actual hostname, you need to substitute your webservers hostname in that spot. – slm Jan 1 '13 at 2:12
work in loclhost, but tell me page Not Found, and displays url as http://localhost/sections/5 but Supposed to be http://localhost/ebdaa_v3/sections/5 – Lion King Jan 1 '13 at 2:23
RewriteRule ^/sections.php$ /ebdaa_v3/sections/%1? [NC,L,R=301] – slm Jan 1 '13 at 2:28

No. The RewriteRule only matters when the user makes a request to the server. It does not affect what they will see when they hover over the link, and it won't change the address that they see from what you change it to to what you change it from (meaning: you're rewriting index/image/ to index.php?usr=image; it won't change it the other way, from index.php to index/image/).

share|improve this answer
Thanks, but Unfortunately, I did not understand well. but when I want to add hyperlink, can I add link like <a href="index/image/">Click Here</a> or leave it as <a href="index.php?usr=5">Click Here</a>. sorry because I understand slowly – Lion King Dec 31 '12 at 23:09
Yes, that would work. The RewriteRule happens whenever they request index/image/, including if they click a link to it. (though, as far as I can tell, what your rule will do is rewrite index/image/ to index.php?usr=image, not index.php?usr=5; I may be wrong, though) – cpast Dec 31 '12 at 23:55
Thanks, so with this way I must change every hyperlink in the script to the new friendly url and this is very difficult. the problem is how can I change all hyperlinks that in the script at once to new friendly url ? – Lion King Jan 1 '13 at 0:01
Only if you want to change all of them. The old URLs will continue to work. – cpast Jan 1 '13 at 0:04
There is no way to change all hyperlink in script to new friendly url ? – Lion King Jan 1 '13 at 0:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.