简体   繁体   中英

Change HTML ID's from url path?

I have 2 sites. We will call them site1.com and site2.com

When a user visits site1.com I would like it to redirect them to site2.com. Easy to do

However, if a person was to visit site1.com/# I would like that to redirect them to site2.com and whatever # they put will be added to the body tags of site2.com as a class. so if they were to go to site1.com/6 <body class="6"> for example. Basically taking the path of the first site and adding that as a class on the body tag of site 2. Can someone point me in the right direction on how to achieve this

This will require collaboration between both domains. if done with JavaScript, not sure about PHP

On site1.com, redirect to a new path with the pathname like so (this is assuming of course, that the page will be shown to all locations matching site1.com/* ):

location = "http://site2.com/"+location.pathname

On site2.com, receive this, then use classList.addClass(name) to document.body like so:

document.body.classList.add(location.pathname);

If you are using php you can get the url path info with $info = $_SERVER['PATH_INFO']; if the URL is 'site1.com/5' the result would be '/5'. After that you can redirect the user with header('Location: site2.com'.$info);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM