HTTP Redirect
HTTP redirection, or URL redirection, is a technique of pointing one domain or address to another.#####
Why do servers use redirect?
- Moving to a different Domain
- Expanding to capture similar domains
- Creating a persistent experience in spite of page name changes(404 Not Found)
- Forcing SSL
Redirect Methonds
Temporary redirects
- Temporary redirects are useful if your web content for a certain URL temporarily needs to be served out of a different location.
- Temporary redirects inform that 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
- Move your content to another location.
Redirect in Apache
- Use the redirect directive
<VirtualHost *:80>
ServerName www.domain1.com
Redirect / http://www.domain2.com
</VirtualHost>
By default, the "Redirect" directive establishes a 302, or temporary, redirect.
Redirect 301 /oldlocation http://www.domain2.com/newlocation
- Use the RedirectMatch Directive
To redirect more than single page, you can use the "RedirectMatch" directive, which allows you to specify directory matching patterns using regular expressions.
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.
RedirectMatch ^/images/(.*)$ http://images.example.com/$1
- Using mod_rewrite to Redirect
Redirect in Nginx
server {
listen 80;
server_name domain1.com;
return 301 $scheme://domain2.com$request_uri;
}
The "return" directive executes a URL substitution and then returns the status code given to it and the redirection URL.
It use the "$scheme" variable to use whatever scheme was used in the original request(https or https). It then returns
the 301 permanent redirect code and the newly formed URL.
- Process a folder redirect to separate subdomain, we can perform a redirection using the "rewrite" directive
rewrite ^/images/(.*)$ http://images.example.com/$1 redirect;
Using redirects correctly will allow you to leverage your current web presence while allowing you modify you site structure as necessary.
=======================================================================================================
Apache Mod_Rewrite
Mod_Rewrite allows you to make custom and simplified URLs as needed.
- .htaccess file
An .htaccess file is a way to configure the details of your website without needed to alter the server config file. The period that starts the file name will keep the file hidden within the folder.
(The .htaccess file location matters. The configurations in that file will affect everything in its directory and the directories under it.)
- Permit changes in the .htaccess file
- To allow the .htaccess file to override standard website configs, start by opening up the configuration file.(AllowOverride All)
After you save and exit that file, restart apache, .htaccess file will now be available for all of your sites.
Rewrite URLS
RewriteRule Pattern Substitution [OptionalFlags]
- RewriteRule: This is the section in which you can write in the name of the mod_rewrite directive that you want to use.
- Pattern: This section is dedicated to interpreting the request URL, using regular expressions.
- Substitution: This is the actual URL of the page with the information we want to display.
- Optional Flags: A flag is a tag at the end of the Rewrite Rule directive that may change the behavior of the expression.
- [F] Making the URL forbidden
- [NC] Forcing the rule to disregard capitalization
- [R=301] or [R=302], controlling the redirect code you want to use
- [L] indicating that this is the last rule in series
RewriteEngine on
RewriteRule ^oranges.html$ apples.html
From: http://example.com/results.php?products=apple
To: http://example.com/products/apple
RewriteEngine on
RewriteRule ^products/([A-Za-z0-9-]+)/?$ results.php?products=$1 [NC]
From: http://example.com/results.php?products=produce&type=fruit&species=apple
To: http://example.com/produce/fruit/apple
RewriteEngine on
RewriteRule ^(meat|produce|dairy)/([^/.]+)/([^/.]+)$ results.php?products=$1&type=$2&species=$3