1

It is known that URL with querystring parameter can be rewritten with .htaccess. For example

localhost/mynews/category.php?cat=news&subcat=9

To

localhost/mynews/news/9

But is there any way to customize anchor tag

<a href="category.php?cat=news&subcat=9"></a>

which is shown in bottom left corner while hovering link.

localhost/mynews/category.php?cat=news&subcat=9

to

localhost/mynews/news/9

PHP, jquery/javascript or htaccess, any way can we customize ?

Dipak
  • 931
  • 14
  • 33
  • 1
    well yeah you could use javascript to alter the href attribute of each and every anchor tag on the page. – Adrian Baginski Jun 15 '18 at 10:06
  • You have to do that in your application. And no, it is not known that entries in your `.htaccess` change the generated markup – Nico Haase Jun 15 '18 at 10:13
  • Your link should look like this in the first place: `< a href="/mynews/news/9">` - otherwise it makes little sense that you implemented the rewriting from `/mynews/news/9` to `category.php?…` to begin with! You need to change this in your HTML, resp. the logic that creates it. – CBroe Jun 15 '18 at 10:50

1 Answers1

1

On hover you can get your anchor href get querystring value rebuild url and set new url using attr in anchor tag.

var link = '';
$("a").hover(function() {
  link = $(this).attr('href');
  var var1 = $.urlParam('cat', link);
  var var2 = $.urlParam('subcat', link);
  var url = link.split('?')[0] + '/' + var1 + '/' + var2;
  $(this).attr('href', url);

}, function() {
  $(this).attr('href', link);
});
$.urlParam = function(name, link) {
  var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(link);
  if (results == null) {
    return null;
  } else {
    return decodeURI(results[1]) || 0;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="category.php?cat=news&subcat=9">Hover Me</a>

Note: I take urlParam function from https://stackoverflow.com/a/25359264/965146 and slightly modified it. Rest code are mine.You must reset your url when anchor click may be routing didn't get your new url

4b0
  • 21,981
  • 30
  • 95
  • 142
  • Thanks, I tried, while running in code snippet, first time it worked, but after first time every time `/null/` is adding while hovering. I thought it was browser `cache` problem, but `clearing cache` and trying in `other browsers`, still same problem – Dipak Jun 15 '18 at 11:01
  • @AnandHmt Because when mouse out i am not reset your url so querystring is null. See updated answer.I reset your url when mouse out. – 4b0 Jun 15 '18 at 11:06
  • Thanks very much, this works, but my requirement is `localhost/mynews/news/9` (I've set url as this in htaccess) as asked in question, but I've got extra `category.php` by your answer `localhost/category.php/mynews/news/9`. How to remove `category.php` ? – Dipak Jun 15 '18 at 11:25
  • Remove `link.split('?')[0] +` from var `url = link.split('?')[0] + '/' + var1 + '/' + var2;` – 4b0 Jun 15 '18 at 11:30