.htaccess redirect

Apache .htaccess 301 redirect is a server side redirect and is a permanent redirect.

The .htaccess file is an Apache server configuration file. The .htaccess file is used per directory.

Using .htaccess file reduce server performance. .htaccess use should be avoided when you have access to the Apache server main configuration file httpd.conf. Shared hosting websites usually don't have access to httpd.conf file and should use .htaccess file.

This 301 redirect response notifies the search engines that the page has moved permanently from the old URL to the new URL.

The search engines also transfer the old URL page rank to the new URL.

.htaccess redirect

Add this code or create new .htaccess file in the old-page.html directory.

Single URL redirect

Permanent redirect from old-page.html to new-page.html.

.htaccess:

Redirect 301 /old-page.html http://www.mydomain.com/new-page.html

Entire domain redirect

Permanent redirect from all domain pages to newdomain.com.

.htaccess file should be at the old website's root directory.

.htaccess:

Redirect 301 / http://www.newdomain.com/

Enabling .htaccess configuration

If you uploaded .htaccess file to the old-page.html directory and the redirection does not work, it usually means that the .htaccess files are not enabled in the Apache server configuration file httpd.conf.

The .htaccess file can be enabled by adding the Apache server's httpd.conf file.

httpd.conf:

<Directory /srv/www/rapidtables.com/public_html/web/dev/redirect>
  AllowOverride All
</Directory>

Important: this setting is not reccomended since it slows down the Apache server.

httpd.conf redirect

If you have permission the change the httpd.conf file, it is better to add the Redirect directive in the httpd.conf instead of the .htaccess file.

Check if rewrite module's library mod_rewrite.so is loaded by the apache server:

$ apache2ctl -M

 

Add the following code to httpd.conf file.

If rewrite module's library mod_rewrite.so is not available, uncomment the first line to load the rewrite module.

httpd.conf:

# LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
<Directory /srv/www/rapidtables.com/public_html/web/dev/redirect>
   Redirect 301 /old-page.html http://www.mydomain.com/new-page.html
</Directory>

 

Don't forget to restart the Apache server after httpd.conf update:

$ sudo /etc/init.d/apache2 restart

 


See also

WEB DEVELOPMENT
CONTACT