如何重定向到主页

如何将用户重定向到主页?

示例: mywebsite.example/ddfdf/fdfdsf和我想重定向到 mywebsite.example

但是,我想在不键入静态名称的情况下执行此操作?

142202 次浏览
document.location.href="/";

maybe

var re = /^https?:\/\/[^/]+/i;
window.location.href = re.exec(window.location.href)[0];

is what you're looking for?

Can you do this on the server, using Apache's mod_rewrite for example? If not, you can use the window.location.replace method to erase the current URL from the back/forward history (to not break the back button) and go to the root of the web site:

window.location.replace('/');
window.location = '/';

Should usually do the trick, but it depends on your sites directories. This will work for your example

strRetMsg ="<script>window.location.href = '../Other/Home.htm';</script>";

Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", strRetMsg,false);

Put this code in Page Load.

document.location.href="/";

or

 window.location.href = "/";

According to the W3C, they are the same. In reality, for cross browser safety, you should use window.location rather than document.location.

See: http://www.w3.org/TR/Window/#window-location

(Note: I copied the difference explanation above, from this question.)

var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;

See this answer https://stackoverflow.com/a/42291014/3901511

window.location.href = "/";

This worked for me. If you have multiple folders/directories, you can use this:

window.location.href = "/folder_name/";

var url = location.href;
var newurl = url.replace('some-domain.example','another-domain.example';);
location.href=newurl;