HTACCESS URL REDIRECT
Posted on July 8th, 2016
In this documentation, we can learn about htaccess and htaccess url redirect.
Htaccess stands for Hypertext access. Htaccess is the oldest configuration file that controls the Web Server running in your website, and is one of the most powerful configuration file. The .htaccess file can be used to alter the Apache Web Server software. It is used to enable/disable features or add functionality that Apache Web server needs. This includes basic redirect functionality, like a 404 file not found error occurs. More advanced functions include content password protection.
HOW TO USE .htaccess
.htaccess is the file name in full, not a file extension. You should not create a file called, ‘file.htaccess’, it is simply called ‘.htaccess’. This file will take effect over the entire directory and loaded via the Apache Web Server software. You can create a .htaccess file using any good text editor such as Text Pad, Ultra Edit, Microsoft WordPad, and anything else that is similar.
Here is an example of what is included in a .htaccess file.
AuthName “Member’s Area Name”
AuthUserFile /path/to/password/file/.htpasswd
AuthType Basic
require valid-user
WHAT IS URL REWRITING?
URL rewrite is one of the best way to improve usability of your site. URL rewriting is the most dynamic and it include variables.
For example, they normally create URL’s like this one for example:
http://www.domain.com/show_a_product.php?product_id=7
HOW IT WORKS
Static URLs are known to be better than Dynamic URLs for a number of reasons.
-> Static URLs typically rank higher in search engines.
-> Compared to static pages, Dynamic URLs appear slower in search engines. They are also known to index the content of dynamic pages.
-> Static URLs are always user friendly.
USING .htaccess REWRITE RULES
The Apache module mod_rewrite will allow you to rewrite the URL request that comes into your server and it is based on the normal. If used properly the mod_rewrite module is powerful.
Example:
.htaccess file will redirect http://example.com/folder1/ to http://example.com/folder2/
Filename: .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1.*$ http://example.com/folder2/ [R=301,L]
.htaccess file will redirect http://example.com/folder1/ to plane http://example.com/
Filename: .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1.*$ http://example.com/ [R=301,L]
a) .htaccess file will redirect http://example.com/folder1/file.html to http://example.com/folder2/file.html
Filename: .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1/(.*)$ http://gs.mt-example.com/folder2/$1 [R=301,L]
Code explanation :
-> Option +FollowSymLinks is an Apache directive.
-> RewriteEngine On enables mode_rewrite.
-> RewriteRule is a particular rule.
-> The first string after RewriteRule defines the original URL.
-> The second string after RewriteRule defines the new URL.
-> [R=301,L] It act as a 301 redirect. It also will stop any later rewrite rules are affecting this URL.
b) .htaccess file will redirect http://example.com/file.html to http://example.com/folder1/file.html
Filename: .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} example.com$ [NC]
RewriteCond %{HTTP_HOST} !folder1
RewriteRule ^(.*)$ http://example.com/folder1/$1 [R=301,L]
Code explanation:
-> Option +FollowSymLinks is an Apache directive.
-> RewriteEngine On enables mode_rewrite.
-> RewriteCond %{HTTP_HOST} It shows which URL we want to run in the rewrite.
-> [NC] Upper & lowercase version of URL matches.
-> RewriteRule is a particular rule.
-> The first string after RewriteRule defines the original URL.
-> The second string after RewriteRule defines the new URL.
-> [R=301,L] It act as a 301 redirect and also stops any later rewrite rules that are affecting this URL.
REGULAR EXPRESSIONS
Rewrite rules contains regular expressions. Regular expression can be difficult to view at first glance. Some common elements you will see in your rewrite rule.
-> ^ Begins the line to match.
-> $ Ends the line to match.
-> . Stands for any non-whitespace character.
-> * Means that the previous character can be matched zero or more items.
-> () Means which portion to dedicate for use again in the $1 variable in the second string.
If you need any further help please reach our support department.
I had a domain hosted here through interserver until covid hit and I lost work, then I couldn’t pay the domain fee…some other company picked it up, isn’t doing anything with it, it just shows a “let us know if you want to try and get this domain, we have brokers who can try to get it from you from the current owner…blah blah blah” for a fee, a fee which I do not want to pay because it seems really underhanded and slimy that someone would do that. I was thinking of creating a new domain or opening up a subdomain with another site I have, is it possible to redirect all my old traffic to the old domain they are holding hostage to my new domain?
Thanks a bunch!