How to redirect a page to another page or website using htaccess?

If a page on your website no longer exists and you want to redirect it to the new page, or else, redirect old website links to the new website, then you can use the .htaccess file for redirection without waiting.

The .htaccess lives in the root of your website's directory, but you can also put it in a subdirectory, wherein this case, all links under this subdirectory will follow that .htaccess file rules. The .htaccess is a hidden file, so first, from the cPanel File Manager, edit the settings to show hidden files:

After making the htaccess file visible, now open and edit it:

Redirecting all URLs:

If you changed your website domain, you can then use the following code to redirect all URLs on your site to the new site.

Redirect 301 /

Redirecting a single URL:

Using the following code, you will be able to redirect a specific page to another one.

Redirect /path/to/old/file/old.html

Redirect non-existing pages to a specific location:

The following code will allow you to redirect any request for a non-existing page, to your index.php file (or any index file) 

Options +SymLinksIfOwnerMatch 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Forcing HTTPS:

To force requests from HTTP to HTTPS, follow this guide:

How to Force HTTPS via htaccess

Forcing www in the URL:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

Forcing non-www in the URL:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]


Was this answer helpful? 0 Users Found This Useful (1 Votes)