htaccess将404页面重定向到index.php

时间:2016-01-28 01:57:01

标签: php apache .htaccess redirect mod-rewrite

我改变了我的网站&网址结构。 现在我想将旧页面(目前是404错误页面)重定向到我的主页。

网址:

(function(){
var index = 1;
var intervals = {},
    timeouts = {};

function postMessageHandler(e) {
    window.postMessage('', "*");

    var now = new Date().getTime();

    sysFunc._each.call(timeouts, function(ind, obj) {
        var targetTime = obj[1];

        if (now >= targetTime) {
            obj[0]();
            delete timeouts[ind];
        }
    });
    sysFunc._each.call(intervals, function(ind, obj) {
        var startTime = obj[1];
        var func = obj[0];
        var ms = obj[2];

        if (now >= startTime + ms) {
            func();
            obj[1] = new Date().getTime();
        }
    });
}
window.addEventListener("message", postMessageHandler, true);
window.postMessage('', "*");

function _setTimeout(func, ms) {
    timeouts[index] = [func, new Date().getTime() + ms];
    return index++;
}

function _setInterval(func, ms) {
    intervals[index] = [func, new Date().getTime(), ms];
    return index++;
}

function _clearInterval(ind) {
    if (intervals[ind]) {
        delete intervals[ind]
    }
}
function _clearTimeout(ind) {
    if (timeouts[ind]) {
        delete timeouts[ind]
    }
}

var intervalIndex = _setInterval(function() {
    console.log('every 100ms');
}, 100);
_setTimeout(function() {
    console.log('run after 200ms');
}, 200);
_setTimeout(function() {
    console.log('closing the one that\'s 100ms');
    _clearInterval(intervalIndex)
}, 2000);

window._setTimeout = _setTimeout;
window._setInterval = _setInterval;
window._clearTimeout = _clearTimeout;
window._clearInterval = _clearInterval;
})();

http://www.example.com/abc-def-ghi-sp(m).html

我在htaccess中使用了以下规则

http://www.example.com

上述规则运作正常。 我还需要另一个重定向。但它无法正常工作

RewriteEngine On
RewriteRule ^(.*)\.html$ / [L,R=301]

http://www.example.com/page.php?name=amca&PID=143&PageID=abc

我在htaccess中使用了以下规则

http://www.example.com

但结果

显示主页。

但是浏览器中的网址是

RewriteRule ^page\.php / [L,R=301]

我尝试了不同的规则。但没有工作

2 个答案:

答案 0 :(得分:0)

尝试^page\.php*$ / [L,R=301]

您只匹配您感兴趣的模式,引擎将仅替换匹配的部分。所以你应该在感兴趣的模式之后匹配所有内容,以免它成为替代品的受害者。

“*”匹配“php”之后的所有内容,“$”表示字符串结束。

答案 1 :(得分:0)

尝试在目标网址的末尾添加

RewriteRule ^page\.php /? [L,R=301]

最后的空问号非常重要,因为它会丢弃原始查询字符串。

这会将/fill.php?foo=bar重定向到/不附加旧的查询字符串。

在测试之前清除浏览器的缓存。

如果您想使用查询字符串重定向空间网址,

您可以尝试

RewriteEngine on


RewriteCond %{QUERY_STRING} ^q=foo&u=bar$ [NC]
RewriteRule ^file.php$ /? [NC,L,R]

这会将特定网址/file.php?q=foo&u=bar重定向到/

相关问题