How do I stop mod_rewrite from appending links to url?

前端 未结 3 1650
别跟我提以往
别跟我提以往 2020-12-20 10:19

I expect the answer is going to be something so simple I\'ll want to cry, but I can\'t seem to figure it out. I\'m new to mod_rewrite.

I wanted to change my links f

相关标签:
3条回答
  • 2020-12-20 10:37

    Whatever you do, clicking a <a href="about/">about</a> will append about/ onto the end of the URL. That's how relative links work.

    Your choices are, in order of sensibleness:

    1. Just remove that trailing slash. That's the cause of your problem:

      <a href="about">about</a>
      

      A relative link will replace the last section of the path (after the last /) with your new value.

    2. Add a preceding ../. This is a bit hacky, but it lets you keep that valuable trailing slash

      <a href="../about/">about</a>
      
    3. Do a 301 redirect from /about/about to /about. This will cause the address bar to change from /about to /about/about and back again.

    0 讨论(0)
  • 2020-12-20 10:47

    Maybe a bit too late to answer, but adding this one line in the <head> section would do the trick:

    <base href="/"> or <base href="your-domain-name">
    
    0 讨论(0)
  • 2020-12-20 10:49

    You are defining all of your links in HTML relative to the current path.

    You will need to change your links such that:

    <a href="about/">about</a> | <a href="contact/">contact</a><br><br>
    

    becomes (note the leading / on the urls):

    <a href="/about/">about</a> | <a href="/contact/">contact</a><br><br>
    

    When you are on a page site.com/about/us a link like <a href="home/" gets resolved by the browser to be site.com/about/us/home.

    The solution is to change all of your links, images, stylesheets, and javascripts to use absolute paths in your URLs, not relative ones like you have now.

    EDIT: Just noticed your edit. You really should use absolute paths, not relative ones. If you want to keep the relative URLs then you will have to use something like <base href="/" /> on all of your pages.

    0 讨论(0)
提交回复
热议问题