HTML remove url variables, NO PHP or JS solution

73 views Asked by At

My site uses both PHP and the JS AJAX so I'm fairly familiar with them both, and I don't want a solution that includes them. I have this page structure where all my users stay on just one landing php page, which then fetches the right content depending on the URL's p variable.

http://www.example.com/?p=about
http://www.example.com/?p=aMap-anothermap-evenAnothermap-lastelyTheFile

This page structure works great for me except that I don't know the right way to make a link that just removes the whole ?p=home. Because I want my home/start page to be variable free. I want it to be

http://www.example.com/

rather than

http://www.example.com/?p=home

Now I could just make the link

http://www.example.com/?

And then just remove the ? with the JS pushState(), but that would look pretty silly and would only work for JS users.

Let's say i would want to the do the above example with just the ? then I could create a link like this.

<a href="?">Link</a>
<script src="SomeCoolPushStateScript"></script>

And I know from experience that this doesn't even work:

<a href="">Link</a>

So here comes the question: How do I remove the ?variable=something part of an URL when using an HTML href?

2

There are 2 answers

3
Alexander O'Mara On BEST ANSWER

The path ./ should do the trick.

<a href="./">Link</a>

If you want to preserve the main script name, like index.php, you will have to include that name.

<a href="index.php">Link</a>

Alternately, you could dynamically generate domain-relative or absolute URL's with PHP.

3
Punit On

You don't need to use querystrings.

<a href="/">Link</a>

would go to example.com's root.

I don't recommend using "./". This would do what you want if the user is on a page that is in the root directory of your website (e.g. http://www.example.com/page.html). However, this would not work if they were on a page in a subdirectory. E.g. if the user's on http://www.example.com/hello/page.html, it would just link to http://www.example.com/hello/.

Using "/" makes sure the user goes to the root of your website.