简体   繁体   中英

HTML remove url variables, NO PHP or JS solution

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 ?

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.

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 (eg http://www.example.com/page.html ). However, this would not work if they were on a page in a subdirectory. Eg 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.

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