广告位

您现在的位置是:主页 > 伪静态 >

ecshop/大商创B2B2C程序动态强制跳转伪静态改造(javascript版)

2020-05-31 22:09伪静态 人已围观

简介思路1:使用$query_string进行301; if ($query_string ~ id=([0-9]+)) { set $id $1; rewrite ^/article.php$ /article-$id.html? permanent; } if ($query_string ~ id=([0-9]+)) { set $id $1; rewrite ^/article_cat.php$ /article_cat-$id.html?...

思路1:使用$query_string进行301;

if ($query_string ~ "id=([0-9]+)") {
    set   $id   $1;
    rewrite ^/article.php$ /article-$id.html? permanent;
}

if ($query_string ~ "id=([0-9]+)") {
    set   $id   $1;
    rewrite ^/article_cat.php$ /article_cat-$id.html? permanent;
}

if ($query_string ~ "id=([0-9]+)") {
    set   $id   $1;
    rewrite ^/goods.php$ /goods-$id.html? permanent;
}

if ($query_string ~ "id=([0-9]+)") {
    set   $id   $1;
    rewrite ^/category.php$ /category-$id.html? permanent;
}

修改服务器上的/etc/nginx/site-available/vhost.conf文件;将上述代码放入

location ~ .php$ {代码后;

测试表明确实能将以下动态地址都跳转到静态地址;

/article.php?id=207   >   /article-207.html

/article_cat.php?id=1012   >   /article_cat-1012.html

/goods.php?id=1138   >   /goods-1138.html

/category.php?id=858   >   /category-858.html

但是这些静态地址本身就是伪静态;不是物理文件;所以跳转后页面一直循环;我们看到的就是无法打开的结果;

基于上述需求;我们先将刚刚改的规则注释掉;

改用javascript跳转实现;

article.dwt:

<link rel="canonical" href="https://www.test.com/article-{$article_id}.html">

    <script type="text/javascript">
        if (document.URL != 'https://www.test.com/article-{$article_id}.html' ){
            window.location.href='https://www.test.com/article-{$article_id}.html'+window.location.hash }
    </script>

 

article_cat.dwt:

<link rel="canonical" href="https://www.test.com/article_cat-{$cat_id}.html">

<script type="text/javascript">
        if (document.URL != 'https://www.test.com/article_cat-{$cat_id}.html' ){
            window.location.href='https://www.test.com/article_cat-{$cat_id}.html'+window.location.hash }
    </script>

 

goods.dwt:

<link rel="canonical" href="https://www.test.com/goods-{$goods_id}.html">​

<script type="text/javascript">
        if (document.URL != 'https://www.sdfaal.com/goods-{$goods_id}.html'){
            window.location.href='https://www.test.com/goods-{$goods_id}.html'+window.location.hash }
    </script>

 

category.dwt:

​<link rel="canonical" href="https://www.test.com/category-{$category}.html">

​<script type="text/javascript">
        if (document.URL != 'https://www.test.com/category-{$category}.html'){
            window.location.href='https://www.test.com/category-{$category}.html'+window.location.hash; }
    </script>

设置好之后,测试;能成功跳转;

出现了一个新增需求;就是tg=baidu-hexinci-chanpin1这种标签无法传递;

我们索性研究了一下javascript的URL传值参数;

window.location.href    (获取整个 URL)
window.location.protocol    (获取http或https协议头)
window.location.host    (获取域名)
window.location.port    (获取端口号)
window.location.pathname    (获取物理页面路径部分))
window.location.search    (获取?后的字符串)
window.location.hash    (获取#后面字符串)

.substr(1).match(变量名)
.split 字符串

参照:地址1  地址2

这时候上面代码中的​+window.location.hash即可取到#后的标识符

https://www.test.com/category.php?id=858#tg=baidu-hexinci-chanpin1

跳转之后

https://www.test.com/category-858.htmltg=baidu-hexinci-chanpin1

糟糕,页面不存在;

把+window.location.hash改成+?window.location.hash

跳转后就是

https://www.test.com/category-858.html?tg=baidu-hexinci-chanpin1

成功带标签动态跳转到静态;

附 ECSHOP/大商创B2B2C软件的增补伪静态规则;(本博客整理)

rewrite ^/account_safe.html$              /user.php?act=account_safe last;
rewrite ^/affiliate.html$              /user.php?act=affiliate last;
rewrite ^/order_list.html$              /user.php?act=order_list last;
rewrite ^/login.html$              /user.php last;
rewrite ^/register.html$              /user.php?act=register last;
rewrite ^/phone_login.html$              /user.php?act=phone_login last;
rewrite ^/get_password.html$              /user.php?act=get_password last;
rewrite ^/flow.html$              /flow.php last;
rewrite ^/favorites.html$              /user.php?act=collection_list last;
rewrite ^/message_list.html$              /user.php?act=message_list last;
rewrite ^/download.html$              /download.php last;

Tags:

广告位
    广告位

本栏推荐

    广告位

标签云

站点信息

  • 文章统计12篇文章
  • 标签管理标签云
  • 微信公众号:扫描二维码,关注我们