简体   繁体   中英

Can I modify anchor tag href in htaccess?

Not sure if this is possible but wanted to know if htaccess has a trick...

Say I have anchor tag href like this -

<a href="http://example.com/index.php?id=12345">Click here</a>

I have changed the URL structure using mod_rewrite but wanted to know if i can hide the actual URL in href using htaccess.

ie when a user hover over the anchor tag, the browser displays http://example.com/index.php?id=12345 at the bottom. All I want the browser to display is http://example.com/index/12345 without changing it manually on all my pages.

Thanks for the help !

Why don't you change the link to the following?

<a href="http://example.com/index/12345">Click here</a>

As you can change the .htaccess I expect that you own or adminstrate this domain. So it should be possible.

If the links are generated by PHP code, then I suggest you to implement and use a translation function like:

function beautify($ugly) {
    // your logic comes here
    return $nice; // ;)
}

... and wrap it around the existing code that currently outputs the urls. This would have two advantages:

  • It's easy and more failsafe to migrate to the new url scheme
  • From now on you have control over all url related code using a single function

I agree, htaccess can't help you. I guess you'll have to change them manually. I wish I could be of more help

No. htaccess is for processing input to the web server, not data sent back from the server.

If you use jQuery you could have it rewrite the href when the page loads using something like this.

$(function(){
    $("a").each(function() { 
      this.href = 'some_new_url that you made based on the one in this.href';
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM