Redirecting a Web Folder Directory to another Directory in htaccess

Redirecting within the same domain

Using htaccess in your root level of your web server, how you redirect one page to another is:

RewriteRule ^url-string-to-redirect$ http://www.yourdomain.com/your-new-url-string [R=301,L]

Or

Redirect 301 /path/to-old-url    http://www.cyourdomain.com/path/to-new-url

To redirect the contents of a whole directory to another use the below:

RewriteRule ^subdirectory/(.*)$ /anotherdirectory/$1 [R=301,NC,L]

To redirect the contents of a whole directory to the webserving root:

RewriteRule ^subdirectory/(.*)$ /$1 [R=301,NC,L]

To redirect the contents of a subdirectory to another domain but in the same subdirectory

 Redirect 301 /subdirectory http://www.anotherdomain.com/subdirectory

Make sure that the opening of the .htaccess file contains the 2 lines of code below which enables the Apache module to rewrite the URLS, then place your redirections below them

Options +FollowSymLinks
RewriteEngine On

 

Redirecting to a different domain

Redirect URLs From Old To New Domain Using 301 Redirects In .htaccess

When you need to switch a website from an old domain to a new domain, you need to redirect all your page URLs, this is when htaccess is your friend.

The code below will create 301 url redirects for both the www and non-www version of ‘olddomain.com‘ to the new domain ‘newdomain.com‘.

Add this .htaccess file to the OLD site webroot and upload the files from the old site to the new to see a seamless switch from an old domain to a new one.

So the example below is redirecting all URLs from olddomain.com to newdomain.com, this is also the 301 redirect to use when using Googles Change of Address tool in Search Console.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301,NC]

The above will use the non-www as preference.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

The above here will redirect 301 all urls from one domain to another but give preference to the www version.

You can also apply this to a subdomain – so the example below is redirecting all URLs from subdomain.olddomain.com to subdomain.newdomain.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain.olddomain.com$ 
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L] 
RewriteCond %{HTTP_HOST} ^www.subdomain.olddomain.com$ 
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]

You can also mask a domain which is like a redirect but keeps the old domain URL but shows the remaining part of the new URL of the new domain – example …

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com
RewriteRule ^(.*) http://newdomain.com/$1 [P]

The snippet below will redirect all the urls from one domain to another – so handy when you are changing domain name but keeping the same content and structure, in the the below the target URL will be non-WWW and prefer HTTPS:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ https://newdomain.com/$1 [R=301,L]

Force a Directory Folder or WebSite to go over HTTPS SSL with htaccess

To force a website to use the secure protocol SSL running the whole site over HTTPS you can make a simple edit to the .htaccess file in the document root.

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

To force a particular folder or directory to serve over SSL, create a .htaccess file in that folder and apply the following to it:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} somefolder 
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]

This assumes SSL is enabled on the domain on an Apache Web Server with the mod_rewrite module enabled.

 

Redirecting WWW to non-WWW with htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Just make sure you add it at the very top of the htaccess file.

3 Comments

  1. Davi Ribeiro on February 18, 2022 at 11:13 am

    Im not so experienced in htaccess and have a question.
    I have a site witch have some directory I want to redirect to home if accessed by url. The .htaccess file to prevent this should be in this direcory? Or in the root directory of the site?

  2. Joseph on October 24, 2021 at 11:16 pm

    I put the following code in the .htaccess file and it worked, however when my client tested it later that day, it stopped working. So I logged in again to see that the code was removed.

    The problem is, if you search for the domain https://thepcpusa.com/ on google, it comes back with an archive link to some directory. So as a temporary fix, I used your code above to redirect that user to the home page.
    So any advice you can provide if this code gets removed again? thanks

  3. Eric on September 8, 2021 at 9:54 am

    I have a question

    When a user opens the url www.domain.com/param

    I want it redirected to www.domain.com?param while masking the new url (the URL address displayed remains unchanged)

    So that instead of accessing param as a folder, it accesses it as a url GET parameter

    Is that possible using .htaccess?

Leave all Comment