47

I know there are a lot of tricks for doing links, for example <a href="?query=string"> will link to the current page after appending the query string. Is there a way to link back to the current page, after removing the query string without just typing the file name?

Example, at the page foo.php?q=3, I want to link to foo.php. Is there a shortcut-type way to do this? The file will be renamed several times, so I don't want to type a bunch of links and then have to edit them later.

Edit: Even though these are PHP files, I'm trying to avoid a server-side solution for this particular problem.

Alex S
  • 25,241
  • 18
  • 52
  • 63
  • p.s. to anybody thinking about anchors.....apparently the community dislikes them soo much that anyone posting them as a solution will be downvoted into the abyss of angle brackets – BozoJoe Nov 11 '15 at 06:23

5 Answers5

74
href="?"

Not exactly what you are after - there's still a question mark at the end - but functionally equivalent.

Alex Barrett
  • 16,175
  • 3
  • 52
  • 51
  • 1
    Even though this is old, I'm upvoting and commenting since it's still relevant, and it's the only non-server-side way to do this (that I can tell) which works on both extension and extension-less URLs. – trnelson Jul 22 '15 at 15:42
  • @trnelson Thanks to Javascript this is not the only client-side way to do this, please see my answer here: https://stackoverflow.com/a/50759516/2161614. – Michaël van de Weerd Jun 08 '18 at 11:25
18

You can use <a href="."> to reload the current page, removing the query string.

See this for a more detailed answer.

Community
  • 1
  • 1
Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
  • 3
    This doesn't work with extensionless URLs, unfortunately. This is the technique I used exclusively until I ran into this issue. – trnelson Jul 22 '15 at 15:40
5

I know it's almost 10 years later, but another way to do this client-side is to use Javascript, e.g.

<a href="javascript:window.location.href=/^[^?]+/.exec(window.location.href)[0]">Anchor</a>

Not very pretty, but it does result in a clean URL when clicked.

0

Are you wanting a client-side equivalent to $_SERVER['PHP_SELF']? Something that will always link to the file with no extra stuff after the extension?

I'm not sure if this works in all browsers, but it works great in Firefox 3.5:

<a href="">Foo</a>
Anthony
  • 36,459
  • 25
  • 97
  • 163
  • 1
    `Foo` Unfortunately, it does not work in IE. From: http://msdn.microsoft.com/en-us/library/cc848861%28v=vs.85%29.aspx - _If HREF is specified as a blank value (href="" or href=), executing the link might display the directory containing the current document, or it might generate an error, depending on other elements in the document and the server environment._ – Bohdan Lyzanets Oct 02 '13 at 09:39
-2

You can link to the current page you're on with:

<a href="#">blah</a>
Tim
  • 813
  • 5
  • 14