Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to convert the links(hyperlink) in my script to friendly-url using mod_rewrite, and I have the following example.

.htaccess Code:

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

PHP, html Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <?php 
          echo $_GET['usr'];
        ?>
        <a href="index.php?usr=10">Click Here</a>
    </body>
</html>

What I need is this code(hyperlink) when click on it.

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

Link appears as follows in the address bar

domain.com/project/index/10  // `project` is the main folder of the script

But unfortunately, when I click on the previous link, appears in the address bar as follows

domain/project/index.php?usr=10

And now, what is the reason of appear the link as old form(without friendly-url) instead of appears as (friendly-url) ?

share|improve this question

2 Answers 2

Change your code to use the URL you want:

<a href="index/10">Click Here</a>
share|improve this answer
    
Thanks, but the problem is, I have many links(hyperlink) in my script like that, and that need to modified every hyperlink in script to friendly-url, to appears in address bar as friendly-url, and this requires effort to do so. Is there a way to do this programmatically. –  Lion King Jan 2 '13 at 11:51
    
Not with mod_rewrite. sed -i 's/href="index.php?usr=([0-9]*)"/href="index/\1"' <FILES> might do the trick to change all your files. That regex may need modifying to avoid matching the wrong parts and may fail entirely. –  Ladadadada Jan 2 '13 at 12:06
    
I'm sorry but I don't understand what is mod_sed, can you please write the Appropriate .htaccess code for my case in your answer not in a comment. thank you –  Lion King Jan 2 '13 at 14:35
    
Not mod_sed, just sed. It's a command line program. And I just realised I missed the /g off the end of that sed command. –  Ladadadada Jan 2 '13 at 14:59
    
I'm sorry, but this is the first time I hear about sed. Where do you write these this code sed -i 's/href="index.php?usr=([0-9]*)"/href="index/\1"' <FILES> ? –  Lion King Jan 2 '13 at 15:19

Mod_rewrite works on the urls being sent to your server from a browser. It does not rewrite the urls on the way out to the user. As Ladadadada said, you'll need to change the urls your application is sending the user or use a different solution like mod_sed to rewrite the urls on their way out to the browser.

share|improve this answer
    
Thanks you, but the problem is, I have many links(hyperlink) in my script like that, and that need to modified every hyperlink in script to friendly-url, to appears in address bar as friendly-url, and this requires effort to do so. Is there a way to do this programmatically. –  Lion King Jan 2 '13 at 11:52

Your Answer

 
discard

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.