You can use .htaccess to automatically redirect a visitor to another location, for example when you have a new website, or if your site is still under construction.
In this guide you can find a description of some of the most commonly used functions and how to implement them on your web space at One.com.
Tip: If you want to redirect your whole website to another domain or ip-address, we recommend using DNS instead. Check out our guide on how to manage your DNS settings.
Redirect 301
You can permanently redirect your visitors to another location, this is called Redirect 301.
The number 301 stands for the http status code. Adding the number 301, tells search engines that the address has permanently moved to another location, consequently your page ranking will also be moved.
Redirect 301 /oldpage.html https://www.one-example.com/newpage.html
Replace /oldpage.html with the current page name and https://www.one-example.com/newpage.html with the new location. Make sure all text is on one line.
Note: In some cases you might want to redirect your visitors only temporarily, for example to an under construction page. In that case it is better to use another code, use number 307 instead of 301.
Redirect to www.yourdomain.com
You might want to do this to improve your search engine ranking. Search engines recognize www.yourdomain.com and yourdomain.com as two different pages, it is better if only one page is used.
#Rewrite to domain with www RewriteEngine On RewriteCond %{HTTP_HOST} !^(.+\.)([a-zA-Z0-9-]+\.([a-z]{2,4})|co\.uk|me\.uk|org\.uk|priv\.no)$ RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+\.([a-z]{2,4})|co\.uk|me\.uk|org\.uk|priv\.no)$ RewriteRule ^(.*)$ https://www.%1/$1 [L,QSA,R=301]
Redirect to yourdomain.com
Similar to redirecting to www, but the other way around. It serves the same purpose.
#Rewrite to domain without www RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.(.*) RewriteRule ^(.*) https://%1/$1 [QSA,L,R=301]
Rewrite everything to HTTPS
If you have activated SSL on your web space you can rewrite all traffic to https://. That way you can make sure that your visitors only use a secure connection. If you only want to redirect to https:// in certain folders on your site, you can add the .htaccess file to that specific folder.
#Rewrite everything to https RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Related articles: