【问题标题】:RewriteRule with url params带有 url 参数的 RewriteRule
【发布时间】:2017-03-22 02:29:05
【问题描述】:

我知道有一千个重复的帖子,但到目前为止对我没有任何帮助。

我正在尝试使用重写规则将/articulo.html?id=friendly-url 转换为/articulo/friendly-url

这是我在 .htaccess 中使用但没有成功:

RewriteEngine On
RewriteRule ^articulo/(.*)$ /articulo.html?id=$1 [L]

编辑:

这是寻找id参数的js:

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;
        console.warn(sPageURL);
        console.warn(sURLVariables);

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

【问题讨论】:

    标签: javascript .htaccess url-rewriting


    【解决方案1】:

    这样试试吧,

    Options -Multiviews 
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^articulo/(.*)$ articulo.html?id=$1 [L]
    

    【讨论】:

    • 不,结果是一样的。它加载了 articulo.html,由于路由改变,没有 css 和 js。查找 id 参数的 js 也不起作用。
    • 要正确加载 css 和 js,请在 head 部分使用 &lt;base href="domain name/"&gt;
    • 这行得通,但它并不能解决由于 url 更改而导致脚本无法检查 id 的问题。我想这是预期的方式,我需要重构代码以在 /? 之后查找 id
    • html页面中id的值是怎么得到的?
    • 我使用查找 id 的脚本编辑了我的帖子。
    猜你喜欢
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    相关资源
    最近更新 更多