How to use htaccess to rewrite url to html anchor tag (#)

后端 未结 4 474
长发绾君心
长发绾君心 2020-12-11 01:22

I have a situation where I want to take the following URL:

/1/john

and have it redirect using Apache\'s htaccess file to go to

/page.php?id=1&na

4条回答
  •  有刺的猬
    2020-12-11 01:40

    What you want to do, can be accomplished with URL rewriting, or, more specifically, URL beautification.

    I just quickly found this well explained blog post for you, I hope it can help you out with the learning to rewrite URLs-part.

    As for the #-thing (expecting that you now know what I'm talking about), I don't see a problem in passing the same variable to the rewritten URL twice. Like: (notice the last part of the first line)

    RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$  /$1/$2/#$2 [R]
    RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&subpage=$2
    

    Though, you'll have to escape the #-part, and it seems that it can be done this way:

    RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$  /$1/$2/\%23$2 [R,NE]
    

    BTW, URL rewriting is not that hard (but can become complicated, and I'm not an expert), but Google can help a lot along the way.

提交回复
热议问题