There are several ways to redirect a URL. This is useful for those looking to redirect requests from their main domain to another directory under that domain. For example, such as directing requests for example.com to example.com/blog. This can be done using an .htaccess file. However, it's also possible to redirect using PHP.
This article gives you a quick example of how to create a redirect using PHP.
Does redirecting work with https (SSL certificate)?
No. Redirecting only works for 'http' URLs. If you enable an SSL certificate on 'example.com' and visit 'https://example.com', you will be redirected to the 'Site Not Found' page. This also applies to redirects created in the DreamHost panel.
Creating a simple redirect in PHP
- Create a file named index.php. You can do this by using an FTP client such as Filezilla, or by using SSH. View the following articles for instructions on how to create it (depending on if you're using an FTP client or SSH):
- Make sure the file is in the domain's directory. This directory is the one specified when adding the domain to your panel. You can also confirm by navigating to (Panel > 'Domains' > 'Manage Domains') and clicking the Edit button to the right of the domain.
In the example above, the domain is websitehelp.support. Your domain shows instead when viewing your settings page.
- In this index.php file, add the following line of code:
<?php header("Location: http://www.example.com/blog"); ?>
Remember to change the sample URL to the URL you would like the site to forward.
You'll then have a simple and quick redirection that you can use to seamlessly redirect an HTTP request to any URL.
- How can I redirect and rewrite my URLs with an .htaccess file?
- SSH overview
- Creating and editing a file via FTP
- Creating and editing a file via SSH