Blog : SSL Htaccess Redirect

SSL Htaccess Redirect

images

SSL Htaccess Redirect

01-06-2021,    EvermolPro
64 Views

Htaccess Redirect

You can redirect users from an old website to a new one without preserving the old page using Redirect in an .htaccess file. You may set up a redirect to send users from index.html to home.html if you use index.html as your index file and later rename index.html to home.html. Consider the following scenario:

Redirect to a local site file

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

Redirect to an external site file

Redirect /path/to/old/file/old.html example.com/new/file/new.html

The old file's first path must be a local UNIX path, not the whole path. If the .htaccess file is located in the directory /example.com, the local UNIX path would not include /home/username/example.com. The directory example.com is represented by the first /. You would replace the / with the old file name if the old file were in that directory. The second path to the new file can be a local UNIX path or a full URL linking to a page on a different or the same server. Mod rewrite is an Apache module that uses a regular-expression parser to rewrite URL requests that arrive on your server. The following examples demonstrate how to:

Direct requests for one subdirectory to a different subdirectory or the primary directory (document root)

Example: example.com/folder1/ becomes example.com/folder2/ or just example.com/.

Direct requests to a subdirectory

Example: example.com/file.html becomes example.com/folder1/file.html.

Add www to every request.

Example: example.com becomes example.com. Or, convert http:// to https://.

Convert URL to all lowercase using Rewrite Map

Example: YourDomaIn.com/recIpeS.html becomes yourdomain.com/recipes

This will help prevent typos from producing HTTP errors.

Create plain text.htaccess file (for more information on this type of file, visit the link), or add the lines from the example to the beginning of an existing.htaccess file. In your file, paste the lines from the corresponding sample. Please note that you should substitute your information for the example text. Replace example.com with your domain, folder1 with the name of your folder, file.html with the name of your file, and so on. Make a backup of your modifications. Use or upload the file to the relevant domain's document root. Upload the file to /var/www/vhosts/example.com/httpdocs/ if your domain is example.com. That concludes our discussion. The rewrite rule should take effect soon after you've uploaded the file.

Some Content Management Systems (CMSs), such as WordPress, overwrite.htaccess files with their configurations. You'll likely need to find a solution to rewrite your content from within the CMS in such a situation.

.htaccess

This .htaccess file will redirect example.com/folder1/ to example.com/folder2/. Choose this version if you don't have the same file structure in both directories:

Filename: .htaccess

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^folder1.*$ example.com/folder2/ [R=301,L]

This .htaccess file will redirect example.com/folder1/ to plain example.com/. Choose this version if you want people redirected to your home page, not whatever individual page in the old folder they originally requested:

Filename: .htaccess.

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^folder1.*$ example.com/ [R=301,L]

This .htaccess file will redirect example.com/folder1/file.html to example.com/folder2/file.html. Choose this version if your content is duplicated in both directories:

File name: .htaccess

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^folder1/(.*)$ http://gs.mt-example.com/folder2/$1 [R=301,L]

Test

Upload this file to folder2 (if you followed the first or third example) or your HTML folder (if you followed the second example) with FTP:

 

Filename: index.html

Mod_rewrite is working!

Then, if you followed the first or second example, visit example.com/folder1/ in your browser. You should see the URL change to example.com/folder2/ or example.com/ and the test page content.

If you followed the third example, visit example.com/folder1/index.html. You should be redirected to example.com/folder2/index.html and see the test page content.

Code Explanation

The Apache directive +FollowSymLinks is a requirement for a mod rewrite.

Mod rewrite is enabled when RewriteEngine is set to On.

RewriteRule specifies a specific rule.

The original URL is defined by the first string of characters following RewriteRule. The special characters are explained in further detail towards the end of this page.

The second string defines the new URL following RewriteRule. This is concerning to the document root (HTML) directory, where / refers to the HTML directory itself, as well as subfolders.

The part in parenthesis () from the first string is matched by $1 at the end. This ensures that sub-pages are redirected to the same sub-page rather than the home page. To return to the main page, remove it. (For this reason, it is not included in the first two examples.) Leave this out if the new directory does not contain the same information as the old directory.)

[R=301, L] - executes a 301 redirect while preventing any further rewrite rules from affecting this URL (an excellent idea to add after the last rule). It's at the end of the same line as RewriteRule.

DIRECT REQUESTS TO A SUBDIRECTORY

example.com/file.html becomes example.com/folder1/file.html.

Note: The directory folder1 must be unique in the URL. It won't work for example.com/folder1/folder1.html. The directory folder1 must exist and have content in it.

.htaccess

This .htaccess file will redirect example.com/file.html to example.com/folder1/file.html:

Filename: .htaccess

 

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} example.com$ [NC]

RewriteCond %{HTTP_HOST} !folder1

RewriteRule ^(.*)$ example.com/folder1/$1 [R=301,L]

Test

Upload this file to folder1 with FTP:

Filename: index.html

Mod_rewrite is working!

Then, visit example.com/ in your browser. You should see the URL change to example.com/folder1/ and the test page content.

ADD WWW OR HTTPS

http://example.com becomes http://www.example.com. Or, http://example.com becomes https://example.com.

.htaccess

This .htaccess file will redirect example.com/ to example.com/. It will also work if an individual file is requested, such as example.com/file.html:

Filename:.htaccess

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com [NC]

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

This .htaccess file will redirect example.com/ to example.com/. It will also work if an individual file is requested, such as example.com/file.html:

Filename: .htaccess

RewriteEngine On

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ example.com/$1 [R,L]

 

Test

Visit http://example.com in your browser. You should see that the same page is displayed, but the URL has changed to example.com (first example) or example.com (second example). Also, example.com/file.html will become example.com/file.html or example.com/file.html.

In your browser, take a close look at the new URL. Does it match a file that already exists on the server at the rewrite rule's new location? It's possible that you'll need to broaden your rewrite rule (you might be able to eliminate the $1 from the second string). Rewrites will be directed to the second string's main index page. Alternatively, you may need to copy files from one area to another.

If the URL is just incorrect (e.g., example.com/folder1//file.html - note the two /s), you should double-check your syntax. (mt) Media Temple does not provide syntactic support.

Evermolpro is the best website designing and development company in Noida Ghaziabad and we are always focussed in generating sales of our clients by supporting them in carrying out their business online. Our team consists of professional who are dedicated enough in helping the clients by making them aware of the trends on how to increase the conversion rate of the website.


Enter your mobile no. and we will call you