开发者

Can I rewrite a URL without redirecting?

So I learned from this handy guide how to use .htaccess to redirect pretty URIs like foobar.com/baz to specific content that may reside at foobar.com/bazheader345.php?开发者_Go百科user=baz.

What if I'd like to do the opposite? My anchors are static but I have a lot of content in different files - and none of them has a particularly pretty name. Say I have a page at foobar.com called 123.html and would like the location bar to remain http://foobar.com despite the navigation between files that the user does.

Can this be done via changes to .htaccess? Or simply Javascript?

If it is possible, would it interfere with the browser's history states?


This is really easy to do with HTML frames. Let's say your home page is http://example.com/index.html. Save index.html as home.html and save something like the following in index.html:

<html>
  <head>
    <title>Website title</title>
  </head>
  <body>
    <iframe src="home.html" width="100%" height="100%" frameborder="0" scrolling="no" />
  </body>
</html>

If you don't want to rename your original home page, you could instead stick a DirectoryIndex directive in your .htaccess file. Just save the above code in, say, frame.html, change home.html to whatever filename your index is at, and add:

DirectoryIndex frame.html

to your .htaccess file.

However, you should know that this practice fell out of style over 10 years ago because it has inherent problems. It won't break your history, but it breaks deep linking (that is, one can't bookmark or link to anything other than the home page).

More info:

  • Frames Suck Most of the Time
  • Why Are Frames So Evil?
  • Framing Critique on Wikipedia


You would be much better off creating pretty names for all those other pages, so that you allow your users to link to or bookmark those other pages.... or keep the ugly urls and don't worry about it.

Keeping the same url and displaying different content defeats the entire purpose of a URL, which is to be a unique address for a specific set of information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜