是否有可能在动态网站上进行网址重写? [英] Is it possible to do a url rewrite on a dynamic website?

查看:92
本文介绍了是否有可能在动态网站上进行网址重写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个问题做了很多研究,我不是盲目地问,但我无法理解这个概念。所以我的网站包含一个index.php文件,通过ajax将数据加载到div中,所以页面不会刷新,url也不会改变。我现在知道我需要链接到使用url重写的某些内容。该网站包含帖子,所以例如所有帖子都从数据库中提取,'site.com'是网址。但我希望能够做'site.com/post-one',并有该链接去那个帖子。我想首先我需要追加一个变量到URL的末尾,当这个帖子的动态内容被加载,因此:site.com?post=1所以从那里我可以使用URL重写;我遇到的问题是这个。由于post 1的内容将被加载到一个div中,如果我去了这条路线,并且实现了URL重写,site.com/post-1现在只需要动态地拉动数据,或者页面必须是静态的?

解决方案

你的问题是这必然需要使用散列,而不是GET变量(因为GET需要一个页面刷新,哈希不)。这是通过在JavaScript中使用 window.location.hash 变量完成的,该变量在#后的URL内容发生变化时更新(例如:如果我要更改 http://site.com/#lol http://site.com/#lmao window.location.hash 会从 #lol 更改为 #lmao ) 。哈希常用于基于Ajax的导航站点,比如Twitter(我认为谷歌也在实施它)。



如果你使用jQuery,你应该尝试 jQuery BBQ插件,这将允许您执行散列更改检测等操作,你将不得不自己实现某种类似的引擎,因为它将被用于任何种类的基于散列的导航)。

但是,您应该记住,这与mod_rewrite没有任何关系,因此您不需要添加任何类型的重写规则。您的所有工作(获取数据等)都将通过Ajax XML HTTP请求完成,而不是通常的HTTP请求。使用这个,你可以使你的url看起来像 http://site.com/#!/post/1 (这可以用你喜欢的任何格式,比如 http:// site .com /#!/ p / this-is-the-posts-title )而不是 http://site.com/?post=1 ,虽然你会错过 http://site.com/post/1


I have done much research into the issue, I'm not blindly asking but I can't grasp this concept. So my website contains a single index.php file that loads data into divs via ajax so the page never refreshes and the url never changes. I now know I need links to certain content using url rewriting. The site contains posts, so for instance all posts are pulled from the db and 'site.com' is the url. But I want to be able to do 'site.com/post-one' and have that link go to that post. I am thinking first I need to append a variable to the end of the url when the dynamic content for that post is loaded as such: site.com?post=1 so from there I can use url rewrite; the problem I'm having is this. Since the content for post 1 would be loaded into a div, if I went this route, and implemented the url rewrite, would site.com/post-1 now just pull the data dynamically as well or does the page have to be static?

解决方案

Your problem is that this would necessarily require the use of a hash, rather than a GET variable (because GET requires a page refresh, a hash doesn't). This is done via the usage of the window.location.hash variable in JavaScript, which is updated whenever the URL's content after a # changes (ex: if I were to change http://site.com/#lol to http://site.com/#lmao, window.location.hash would change from #lol to #lmao). Hashes are being used commonly in Ajax-based navigation sites, such as Twitter (I think Google's implementing it as well).

If you're using jQuery, you should try the jQuery BBQ plugin which will allow you to do things such as hash change detection (otherwise, you will have to implement some kind of similar engine yourself, because it will be needed for any kind of hash-based navigation).

You should remember, though, that this doesn't have anything to do with mod_rewrite, thus you shouldn't need to add any kind of rewrite rules. All your work (fetching data, etcetera) would be done through Ajax XML HTTP requests, rather than common HTTP requests.

Using this, you could make your url look like http://site.com/#!/post/1 (this could go whichever format you'd like, such as http://site.com/#!/p/this-is-the-posts-title) instead of http://site.com/?post=1, although you would be missing on http://site.com/post/1.

这篇关于是否有可能在动态网站上进行网址重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆