I'm trying to redirect from my axios call, but window.location.href redirects me in wrong place. Somehow it inserts directory path into url: https://my_site.com/prod/public/en/index /prod/public is the path where my project is located
This happens only in production server. In development environment and in test server, everything is fine. Also I found out that this is happen only when I'm adding index page, for example /en/index if I put /en/product_name it forks fine?!?!?
My code:
return axios
.put(`/lang/` + code)
.then(response => {
console.log(response.data); // en/index
console.log(location.href); // https://my_site.com
window.location.href = "/" + response.data; // redirects me to: https://my_site.com/prod/public/en/index
})
I'm expecting to go to: https://my_site.com/en/index instead of https://my_site.com/prod/public/en/index
What I'm doing wrong?
The problem:
window.location.href= "/" + response.data;
redirects to the root of your page + en/index
. In your case the root is /prod/public
, so this is added to / the starting point of the url.
The solution
You could use location.origin + response.data instead in case you are on a different sub directory
return axios .put(`/lang/` + code) .then(response => { console.log(response.data); // en/index console.log(location.origin); // https://my_site.com var new_location = location.origin+ "/" + response.data; console.log('new location is:' new_location); // to test if the url here is the same as the 1 you get directed to window.location.href = new_location; // redirects me to: https://my_site.com/en/index })
The problem was in .htaccess file.
RewriteCond %{REQUEST_URI} !^/prod/public/
RewriteRule (.*) /prod/public/$1
I still don't know why it puts /prod/public/ only in that route but... I resolved the problem.
Thanks to all you guys!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.