A seven-day limit is to be introduced on the use of temporary accommodation for homeless people in Scotland.
The limit on spending time in unsuitable accommodation currently applies to families and pregnant women but will be extended to everyone at risk of homelessness.
Charities have previously warned people were living in bed and breakfast and hostel rooms for more than a year.
The change in regulations will take place from May 2021.
A Scottish government consultation on the change found 97% of respondents backed the move.
Housing Minister Kevin Stewart said: “We know that people living in these unsuitable environments can for too long often lack cooking or washing facilities, and some have reported that they cannot have visits from family or friends.
“These experiences have a detrimental effect on people’s physical and mental wellbeing, preventing them from rebuilding their lives.
“While temporary accommodation can offer an important emergency safety net for anyone who finds themselves homeless, such as those fleeing domestic violence, it should be a purely temporary measure.”
Mr Stewart said the Scottish government was investing £32.5m in supporting local authorities to “prioritise settled accommodation for all”.
Latest Scottish government figures show that 36,465 people asked for help to find a home from their local council in 2018-19, up 3% on the previous year.
It is the second year applications have risen after a period of consistent decline since 2005-06.
The figures showed also showed increasing numbers of people were also staying in temporary accommodation, such as B&Bs.
There were a total of 620 breaches of the rules on suitable accommodation offered to vulnerable people or families, an increase of 225 compared with the previous year.
A total of 465 of these were in Edinburgh.
Jon Sparkes, chief executive of homeless charity Crisis, said: “We strongly welcome the announcement that the Scottish government will change the law so that people will no longer have to live in the most unsuitable forms of temporary accommodation for longer than seven days.
“This marks a major achievement for our Life in Limbo campaign, a three-year project which has sought to put an end to lengthy and dehumanising stays in unsupported hostels, hotels and B&Bs.
“This decision is a recognition of the resolve of our clients to shine a light on the inhumane conditions they were experiencing and the determination to ensure no-one else was subjected to these prolonged stays.”