根据点击的link改变url (Javascript html)
Change url based on the clicked link (Javascript html)
我遇到了问题,或者更好的是对如何做有疑问。
解释一下:我有一个类似博客的网站,我在上面写文章,文章显示在首页。我希望每次我点击一篇文章时,它都会将我重定向到显示文章内容的页面。我用标题、日期、内容等将每篇文章保存在数据库中...我的疑问是:我应该为每篇文章创建一个 .html 文件还是只创建一个名为 "article.php" 的文件以及何时创建打开它,它改变了从数据库中获取数据的内容?
第二个问题:我希望页面的 url 根据我点击的文章而改变。例如,我单击名为 "Today and Tomorrow" 的文章,我希望 URL 显示为 "mysite/today-and-tomorrow/",而不是 "mysite/article.php"。
我对这个话题有点困惑,所以如果有人能帮助我,我会很高兴。
感谢您的建议。
我试过了
var link = 'www.example.com/training/product.html';
link.split('.html')[0];
window.history.replaceState( null, null, link );
但它动态地改变了 URL,所以几秒钟后它显示为原来的 URL,在我的情况下它显示为 "mysite/article.php",然后它变为 "mysite/today-and-tomorrow".我认为它不正确。
我也试过了
function openArticle(title){
var rightTitle = $(title).text();
rightTitle = rightTitle.toLowerCase();
rightTitle = rightTitle.substring(rightTitle .indexOf(':')+2);
rightTitle = rightTitle.replace(/[^A-Z0-9]+/ig, "-");
event.preventDefault();
window.location.href = title.href + "/" + rightTitle;
}
但是当我点击元素时 "article.php" 仍然在 url
should I create a .html file for each article or only a file called "article.php" and when I open it, it changes the content getting the data from the database?
不,你不必这样做。您可以将文章 ID 作为“query string”传递给 "article.php",它会负责向最终用户提供适当的文章。
I want that the url of the page changes based of article that I click. For example I click on the article called "Today and Tomorrow", I want that the URL appears as "mysite/today-and-tomorrow/", not "mysite/article.php".
您正在谈论一个名为 "URL Rewriting" 的功能,它可以生成漂亮且有意义的 URL。为了实施它,您需要:
- 以可以将文章 ID 映射到其标题的方式设计您的数据库。(用于创建别名)
- 您必须在您的网络服务器中启用 URL Rewrite。 (例如,如果您使用 Apache 作为 Web 服务器,you need to enable
mod_rewrite
)。
- 您必须将用于映射和路由 nice URL 的规则嵌入到名为
.htaccess
的文件中的原始规则。作为样本 How to write htaccess rewrite rule for seo friendly url
我只是想帮助您从技术角度全面了解您正在寻找的内容,正如您所知,不可能仅用一个 post.
我遇到了问题,或者更好的是对如何做有疑问。
解释一下:我有一个类似博客的网站,我在上面写文章,文章显示在首页。我希望每次我点击一篇文章时,它都会将我重定向到显示文章内容的页面。我用标题、日期、内容等将每篇文章保存在数据库中...我的疑问是:我应该为每篇文章创建一个 .html 文件还是只创建一个名为 "article.php" 的文件以及何时创建打开它,它改变了从数据库中获取数据的内容?
第二个问题:我希望页面的 url 根据我点击的文章而改变。例如,我单击名为 "Today and Tomorrow" 的文章,我希望 URL 显示为 "mysite/today-and-tomorrow/",而不是 "mysite/article.php"。 我对这个话题有点困惑,所以如果有人能帮助我,我会很高兴。 感谢您的建议。
我试过了
var link = 'www.example.com/training/product.html';
link.split('.html')[0];
window.history.replaceState( null, null, link );
但它动态地改变了 URL,所以几秒钟后它显示为原来的 URL,在我的情况下它显示为 "mysite/article.php",然后它变为 "mysite/today-and-tomorrow".我认为它不正确。
我也试过了
function openArticle(title){
var rightTitle = $(title).text();
rightTitle = rightTitle.toLowerCase();
rightTitle = rightTitle.substring(rightTitle .indexOf(':')+2);
rightTitle = rightTitle.replace(/[^A-Z0-9]+/ig, "-");
event.preventDefault();
window.location.href = title.href + "/" + rightTitle;
}
但是当我点击元素时 "article.php" 仍然在 url
should I create a .html file for each article or only a file called "article.php" and when I open it, it changes the content getting the data from the database?
不,你不必这样做。您可以将文章 ID 作为“query string”传递给 "article.php",它会负责向最终用户提供适当的文章。
I want that the url of the page changes based of article that I click. For example I click on the article called "Today and Tomorrow", I want that the URL appears as "mysite/today-and-tomorrow/", not "mysite/article.php".
您正在谈论一个名为 "URL Rewriting" 的功能,它可以生成漂亮且有意义的 URL。为了实施它,您需要:
- 以可以将文章 ID 映射到其标题的方式设计您的数据库。(用于创建别名)
- 您必须在您的网络服务器中启用 URL Rewrite。 (例如,如果您使用 Apache 作为 Web 服务器,you need to enable
mod_rewrite
)。 - 您必须将用于映射和路由 nice URL 的规则嵌入到名为
.htaccess
的文件中的原始规则。作为样本 How to write htaccess rewrite rule for seo friendly url
我只是想帮助您从技术角度全面了解您正在寻找的内容,正如您所知,不可能仅用一个 post.