1

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

Example

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

Or leaving it as is

Example

<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 http://domain.com/index/image/.

EDIT #1

Here's a sample of the html code.

<div id="main_menu"> 
  <ul> 
    <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> 
  </ul> 
</div>
3
  • @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, 2012 at 23:38
  • With this way I must change every hyperlink in the script to the new friendly url http://domain.com/index/image/ 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, 2012 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, 2013 at 0:48

2 Answers 2

2

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

http://some.server/sections.php?cid=5
                   ^^^^^^^^^^^^ ^^^^^
                   ||||         ||||
                   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:

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

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

http://localhost/sections.php?cid=5

Clicking this link takes me to this URL:

http://localhost/sections/5

my /var/www/html has the following:

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

1 directory, 2 files

Resources

6
  • 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 http://domain.com/sections.php?cid=3 .
    – Lion King
    Jan 1, 2013 at 2:05
  • my script inside folder ebdaa_v3
    – Lion King
    Jan 1, 2013 at 2:11
  • localhost isn't a actual hostname, you need to substitute your webservers hostname in that spot.
    – slm
    Jan 1, 2013 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, 2013 at 2:23
  • RewriteRule ^/sections.php$ /ebdaa_v3/sections/%1? [NC,L,R=301]
    – slm
    Jan 1, 2013 at 2:28
1

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/).

5
  • 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, 2012 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, 2012 at 23:55
  • Thanks, so with this way I must change every hyperlink in the script to the new friendly url http://domain.com/index/image/ 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, 2013 at 0:01
  • Only if you want to change all of them. The old URLs will continue to work.
    – cpast
    Jan 1, 2013 at 0:04
  • There is no way to change all hyperlink in script to new friendly url ?
    – Lion King
    Jan 1, 2013 at 0:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .