Tutorial

How To Create Temporary and Permanent Redirects with Apache and Nginx

Updated on March 17, 2022
How To Create Temporary and Permanent Redirects with Apache and Nginx

Introduction

HTTP redirection, or URL redirection, is a technique of pointing one domain or address to another. There are many uses for redirection, and a few different kinds of redirection to consider. Redirects are used whenever a site needs people requesting one address to be directed to another address.

As you create content and administrate servers, you will often find the need to redirect traffic from one place to another. This guide will discuss the different use cases for these techniques, and how to accomplish them in Apache and Nginx, the two most common web servers.

Prerequisites

  • An Ubuntu 20.04 server set up by following the Ubuntu 20.04 initial server setup guide, including a sudo non-root user and a firewall.

  • Apache or Nginx installed on your server, which you can do by following How To Install Apache on Ubuntu 20.04 or How To Install Nginx on Ubuntu 20.04. Both Apache and Nginx can be installed at the same time, and many stacks make use of both servers at once, but by default, they will conflict on the default HTTP/HTTPS ports 80 and 443, so if you are following this tutorial on a stock server configuration, you should only install one at a time.

Step 1 – Reviewing Redirect Methods

There are many use cases for redirects. If you have established a web presence and would like to change your domain, it is best not to just abandon your old domain. Bookmarks to your site and links to your site located on other pages throughout the internet will break if your content disappears without any instructions to the browser about how to find its new location. Changing domains without redirecting will cause your site to lose traffic from previous visitors and lose all of the credibility you have worked to establish.

Often, it is helpful to register multiple variations of a name in order to benefit from users typing in addresses similar to your main domain. For example, if you have a domain called myspiders.com, you might also buy the domain names for myspiders.net and myspiders.org and redirect them both to your myspiders.com site. This allows you to catch users who might be trying to get to your site with the wrong address. It can also help prevent another site from using a similar domain and profiting off of your web presence.

Sometimes, it is necessary to change the names of pages that have already been published and received traffic on your site. Normally, this would lead to a 404 Not Found error or possibly another error depending on your security settings. These can be avoided by leading your visitors to another page that contains the correct content they were trying to access. There are a few different kinds of URL redirects, each of which mean something different to the client browser. The two most common types are 302 temporary redirects, and 301 permanent redirects.

Temporary Redirects

Temporary redirects are useful if your web content for a certain URL temporarily needs to be served out of a different location. For example, if you are performing site maintenance, you may need to use a temporary redirect of all of the pages for your domain to an explanation page to inform your visitors that you will be back shortly.

Temporary redirects inform the browser that the content is temporarily located at a different location, but that they should continue to attempt to access the original URL.

Permanent Redirects

Permanent redirects are useful when your content has been moved to a new location forever.

This is useful for when you need to change domains or when the URL needs to change for other reasons and the old location will no longer be used. This redirect informs the browser that it should no longer request the old URL and should update its information to point to the new URL.

Forcing SSL

A common use for redirects is directing all site traffic to use SSL instead of standard HTTP.

Using redirects, it is possible to make all requests for http://www.mysite.com be redirected to https://www.mysite.com. Using LetsEncrypt to provide HTTPS will automatically generate a redirect configuration like this.

In the next part of this tutorial, you will learn some example configurations for redirects using the Apache web server. If you are using Nginx instead and you have no plans to use Apache, you can skip to Step 3.

Step 2 – How to Redirect in Apache

Apache can redirect using a few different tools. Straightforward redirects can be implemented with tools from the mod_alias module, and more extensive redirects can be created with mod_rewrite.

Using the Redirect Directive

In Apache, you can implement single-page redirects using the “Redirect” directive, which is included in the mod_alias module that is enabled by default. This directive takes at least two arguments: the old URL and the new URL.

Apache server blocks can be stored in the primary Apache settings file in /etc/apache2/apache2.conf, but are more maintainable if you create a new file in /etc/apache2/sites-available/ for each configuration. Apache’s convention is to create symbolic links (like shortcuts) from files in sites-available/ to another folder called sites-available/ as you decide to enable or disable them. By default, Apache is installed with a single site-specific configuration in /etc/apache2/sites-available/000-default.conf which is enabled and linked to /etc/apache2/sites-enabled/000-default.conf.

You can open that configuration using nano or your favorite text editor:

  1. sudo nano /etc/apache2/sites-available/000-default.conf

By default, it contains a standard web server configuration that will listen on port 80 and look for an index.html file located in /var/www/html on your system.

/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
…
</VirtualHost>

If you instead needed to redirect requests for domain1.com to domain2.com, you could remove the existing directives from this server block and replace them with a permanent redirect:

/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
	ServerName www.domain1.com
	Redirect / http://www.domain2.com
</VirtualHost>

<VirtualHost *:80>
	ServerName www.domain2.com
	. . .
	. . .
</VirtualHost>

Save and close the file. If you are using nano, press “Ctrl+X”, then when prompted, “Y” and then Enter.

This redirect instructs the browser to direct all requests for www.domain1.com to www.domain2.com. This is only for a single page, not for the entire site.

By default, the Redirect directive establishes a 302, or temporary, redirect. If you would like to create a permanent redirect, you can do so in either of the following two ways:

/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
	ServerName www.domain1.com
Redirect 301 /oldlocation http://www.domain2.com/newlocation
</VirtualHost>
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
	ServerName www.domain1.com
Redirect permanent /oldlocation http://www.domain2.com/newlocation
</VirtualHost>

Using the RedirectMatch Directive

To redirect more than a single page, you can use the RedirectMatch directive, which allows you to specify directory matching patterns using regular expressions.

This will allow you to redirect entire directories instead of just single files.

RedirectMatch matches patterns in parenthesis and then references the matched text in the redirect using “$1”, where 1 is the first group of text. Subsequent groups are given numbers sequentially.

For example, if you wanted to match every request for something within the /images directory to a subdomain named images.example.com, you could use the following:

/etc/apache2/sites-available/default
<VirtualHost *:80>
…
RedirectMatch ^/images/(.*)$ http://images.example.com/$1
…
</VirtualHost>

As with the Redirect directive, you can specify the type of redirect by adding the redirect code before the URL location rules.

In order for your changes to take effect, you’ll need to restart Apache. On a modern Ubuntu server, you can do this using systemctl:

  1. sudo systemctl restart apache2

Using mod_rewrite to Redirect

The most flexible, but complicated way to create redirect rules is with the module called mod_rewrite. For information on working with mod_rewrite, refer to How to Rewrite URLs with mod_rewrite for Apache.

In the next part of this tutorial, you will learn some example configurations for redirects using the Nginx web server. If you already installed and configured Apache, you will need to have port 80 free before following the configuration steps for Nginx on the same server. You can accomplish this by temporarily uninstalling Apache with apt remove, which will retain your configuration files should you later want to reinstall it, or by changing Apache’s configuration to listen for connections on a port other than 80, which is used by Apache, Nginx, and all HTTP connections by default.

Step 3 – How to Redirect in Nginx

Redirects in Nginx are a core feature and make use of common Nginx directives. Most of the time, you can implement redirects by creating a new server block for each URL.

Nginx server blocks can be stored in the primary Nginx settings file in /etc/nginx/nginx.conf, but are more maintainable if you create a new file in /etc/nginx/sites-available/ for each configuration. Nginx’s convention is to create symbolic links (like shortcuts) from files in sites-available/ to another folder called sites-available/ as you decide to enable or disable them. By default, Nginx is installed with a single site-specific configuration in /etc/nginx/sites-available/default which is enabled and linked to /etc/nginx/sites-enabled/default.

You can open that configuration using nano or your favorite text editor:

  1. sudo nano /etc/nginx/sites-available/default

By default, it contains a standard web server configuration that will listen on port 80 and look for an index.html file located in /var/www/html on your system.

/etc/nginx/sites-available/default
server {
        listen 80 default_server;
        listen [::]:80 default_server;
…
        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
…
}

If you instead needed to redirect requests for domain1.com to domain2.com, you could remove the existing directives from this server block and replace them with a permanent redirect:

/etc/nginx/sites-available/default
server {
	listen 80;
	server_name domain1.com;
	return 301 $scheme://domain2.com$request_uri;
}

Save and close the file. If you are using nano, press “Ctrl+X”, then when prompted, “Y” and then Enter.

The return directive executes a URL substitution and then returns the status code given to it and the redirection URL.

In this case, it uses the $scheme variable to use whatever scheme was used in the original request (http or https). It then returns the 301 permanent redirect code and the newly formed URL.

In order to process a folder redirect to a separate subdomain, you can perform an operation similar to the Apache folder redirection using the rewrite directive. This directive, when placed in a server block, will issue a temporary redirect for requests inside the images directory to the subdomain images.example.com:

/etc/nginx/sites-available/default
server {
…
rewrite ^/images/(.*)$ http://images.example.com/$1 redirect;
…
}

For a permanent redirect, you could change redirect to permanent at the end of the statement.

In order for your changes to take effect, you’ll need to restart Nginx. On a modern Ubuntu server, you can do this using systemctl:

  1. sudo systemctl restart nginx

You can expand on this configuration in many other ways. Using these fundamentals of Nginx in order to produce your desired server configuration can keep your stack very lean, as well as helping to concisely document and handle any potential edge cases around your server traffic.

Conclusion

In this tutorial, you learned to configure both temporary and permanent redirects for popular web servers. Be sure to use the correct redirection type, as an improper use of temporary redirects can hurt your search ranking.

Using redirects correctly will allow you to leverage your current web presence while allowing you to modify your site structure as necessary. If you would like to learn more about configuring redirects in Nginx, you can read How To Create Temporary and Permanent Redirects with Nginx

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar

Senior DevOps Technical Writer



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I have created a permanent redirect inside my ssl config file in sites-available such that all requests to “/” are redirected to my ssl enabled site https://www.domain.com as follows:

Redirect permanent / https://www.domain.com/

As a result Every request to my website is redirecting to the “www” subdomain of my domain which is my desired hostname. BUT when someone types https://domain.com in full, it doesnt redirect to my “www” subdomain as highlighted below :

https://domain.com -> https://www.domain.com [Not happening]

Since, I have ssl certificate for both domain.com and www.domain.com I am able to serve the content fine. But, my Google Analytics is getting affected since its looking at it as requests for two different domains.

I tried playing around with my .htaccess file but could not find a fix.

Please suggest how to fix this issue.

if we want to serve website with subdomain www, we need to configure CNAME record, which is alias of A record, pointing a subdomain to an A record in our DNS. So CNAME should look like this:

www @

What location do we do this in?

This is excellent and clearly written. It would be improved by describing (or linking to) what to do with the directives; i.e. write them in the server configuration, or in .htaccess; restart the server in the former case, &c.

Here is a working redirect for both NON WWW to WWW & HTTP to HTTPS. This covers the entire site. Running Ubuntu 14.04 with Apache. The .htaccess file is located in your root… example —> /var/www/html. The 000-default.conf file is located at /etc/apache2/sites-available/. Replace “yoursite.com” with your domain. Cheers. 03-31-2018.

<!-- .htaccess file -->
 Options +FollowSymLinks -MultiViews

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault A1209600
  ExpiresByType text/html A1
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
	
	RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
	RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301] 

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* loader.php [L,QSA]
    
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    
</IfModule>
<!-- 000-default.conf -->
<VirtualHost *:80>	
    ServerName www.yoursite.com
    Redirect / https://www.yoursite.com/
	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html
    
    <Directory /var/www/html/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
    
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Thanks for this valuable tutorial. I want to improve it more. For Apache web server if you want to redirect all the pages from old location to new location blindly (useful for moving whole website) and want to transfer pages’ credibility, use the following command.

RedirectMatch ^/(.*)$ http://newdomain.com/$1

For permanent redirection use the permanent command like this.

RedirectMatch permanent ^/(.*)$ http://newdomain.com/$1

RedirectMatch does not work for me: Apache httpd.conf: <VirtualHost :80> ServerName richardjaybrown.com RedirectMatch "^/(.)$" “https://richardjaybrown.com/$1” </VirtualHost> When I browse to localhost, the redirect is triggered! Why is localhost considerd the same as richardjaybrown.com?

Hi There,

i am trying to forward all my domains to my marketplace site hosted with DO . Right now i have to manually forward each domain by url redirect or url forwarding to my marketplace.

What i want is to basically forward my domains by simply changing the nameserver, i don’t want to do url redirect or url forwarding or domain alias for each domain.

for example if you go to rackcargo.com

it redirects to for sale page https://www.domainmarket.com/buynow/rackcargo.com

basically they only update the name server and domain is automatically transferred to the right page on the main website.

please kindly let me know if this tutorial above is similar to what i am looking.

Any help is greatly apprecaited. Willing to pay for this solution.

thank you

Nice article, but I still canoot do this, I want to have all http to https and only some paths to http with different ports:

server { listen 80; servername _; return 301 https://$host$requesturi;

location /bamboo { rewrite ^/bamboo(.*)$ http://mydomainname:8085/$1 last; }

location /api { rewrite ^/api(.*)$ http://mydomainname:8080/$1 last; }

return 403; } the above does not work,

also this does not work:

#Redirect paths server { listen 443; server_name _;

location /bamboo { rewrite ^/bamboo(.*)$ http://mydomainname:8085/$1 last; }

location /api { rewrite ^/api(.*)$ http://mydomainname:8080/$1 last; }

return 403; } #Redirect http -> https server { listen 80; servername _; return 301 https://$host$requesturi;

return 403; }

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel