生成页面URLjQuery

时间:2018-07-02 16:44:41

标签: mod-rewrite url-rewriting url-routing url-parameters

我想从索引页面重定向到其他传递ID的页面。 在HTML我有这个代码 我正在使用jquery,并且我有此代码。

$("<a class='theme-btn default-btn' href='#'>Read more </a></button>").appendTo($target).click(function() {
                    document.location.href='post.html?id=' + $id;
                });

重定向后的网址是> http://example.com/post?id=14 我想像这样> http://example.com/post/this-is-the-post-title 我该怎么办?

1 个答案:

答案 0 :(得分:0)

我知道您正在尝试做什么。 您必须使用重写URL在服务器端进行操作。

取决于您的服务器设置,它可以在web.config内部(对于IIS)或在htaccess中(Apache)。

这是一个有关您的问题的IIS重写规则的示例。

<rule name="Category Rule">
    <match url="post/([0-9]+)-([0-9]+)*.aspx" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
    <action type="Rewrite" url="post.aspx?id={R:1}" appendQueryString="true" />
</rule>

URL是否为; /post.aspx?id=14 这将返回URL / post / 14

为此,您的页面需要某种逻辑来确定显示哪些内容,具体取决于页面的ID。例如,数据库的哪一行要获取页面的内容。