As WordPress requires a paid hosting, sometimes you need to install it in a sub domain of the main domain like a diary or blog attached to any domain, in which case it may require redirect to manage for the WordPress site to a new location, some high cost webhosting service providers allow easy one click redirect facility while most of the others who mainly cPanel WHCMS dependant, require manually editing the htaccess file for writing the redirect codes ourselves.
The htaccess file is technically known as distributed configuration file used to manage all the url in the directory it exists, found mostly in the famous Apache web servers and very useful for easy configuration and simple change codes. The codes are normally written in CGI and PHP and can be integrated with all the current content management system like WordPress, Joomla, Drupal etc. but setting the configuration for them is different on each content management software.
It is not at all advisable to edit or modify this htaccess file unless for the redirect reason and even then you should have the knowledge of what you are doing, otherwise wrong modification of this file will lead into website unavailability and other critical errors. The file .htaccess resides in the root folder of the directory and if you have installed WordPress manually or read previous tutorials here you will know that it requires to change the .htaccess.txt file into .htaccess file after it has been uploaded during installing WordPress in the site via ftp. The reason for not editing it before uploading is that it detects the directory in which it is installed which is not possible before installation. So for a complete understanding and redirect codes on htaccess file, let us have a look on the content of the basic file that WordPress provides.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^(.?)$ http://www.yoursite.com/$1 [R=301,L]
</IfModule>
# END WordPress
This lines of code tell the host to use the file for managing url, we will go line by line for understanding file here. First line indicates the WordPress as a site building software, as provided in the file. The mod_rewrite.c allows the WordPress software to rewrite the url on its own purpose which is very important for canonical urls and SEO purpose. The next two lines enable the rewrite program and take the base url from the root directory. Next few lines are used for setting up the main index url of the site as per WordPress compliances and then the code ends.
For redirecting this site to another, you need to add the following code just before the module ends with </IfModule> Syntax.
RewriteRule ^(.?)$ http://www.toberedirectingto.com/$1 [R=301,L]
The above line adds a 301 permanent redirect rule in the htaccess file and redirects the whole site immediately to another site, for example toberedirectingto.com here in the example when any user browse the main site or any link of the site.
Keep reading WordPress Developer for more tips and tricks on managing WordPress sites easily and effectively and don’t forget to subscribe to our newsletter for more tutorials.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.