How to redirect website traffic with .Htaccess


If you are running a website or other application on an Apache web server, you should be able to create and edit the .htaccess files in each directory. You can use these files to perform various directory and file redirects and essential access management functions.

Today we’re going to cover and provide coding and configuration examples for many of the possible uses of redirect.

Enforce the use of www in the url

If you need or want to force the use of www in your website URL, you can do this by changing the htaccess on your main directory. Some search engines, like Google, are sensitive to this and basically treat your URL with the www and without the www as two separate websites. It could also have an impact on your site’s ranking.

Here is the code to add or modify for the htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[nc]
RewriteRule ^(.*)$$1 [r=301,nc]

Remember, insert your real domain where you see it

Redirect the old directory or file to the new one

If you are deleting or modifying directories or files, you might want to insert a redirect to the new content or the index page in a different directory. This is useful if users have bookmarked old content while ensuring that old content remains properly listed in search engines. To redirect a directory, insert the following code into the htaccess file in the appropriate directory:

RedirectMatch 301 /old-directory(.*) /new-directory/$1

To redirect a single file:

Redirect 301 /oldfile.htm /newfile.htm

Remember to insert your real directory or filename for the old and new.

Redirection to a new or different domain

If you move your website to another domain, you might want to automatically transfer users who visit your old domain to your new domain. Or you could have multiple domains and want to redirect to just one. Anyway, here is the code that performs the domain redirect:

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

Remember to insert the real old and new domains in place of the “old example” and “new example” content above.

Create custom error pages

As you probably know, the web server can trigger certain error pages when specific errors occur. Instead of showing the default pages to users, consider creating your own web pages. Here are the main types of errors and the code to point to a custom error page:

Bad Request: ErrorDocument 400 /home/www/400.html
Authentication required: ErrorDocument 401 /home/www/401.html
Prohibited: ErrorDocument 403 /home/www/403.html
Not found: ErrorDocument 404 /home/www/404.html
Internal Server Error: ErrorDocument 500 /home/www/500.html

Depending on your server structure, you may need to change the path: / home / www /. Additionally, keep in mind that you can also create your own file names for these pages.

Change the default directory page

A web server is configured by default to display a certain page (like index.htm, index.html, index.php, etc.) when a user tries to visit a directory instead of a specific file. If you want to customize the file displayed when a user visits a directory, add the following code to the htaccess file in the desired directory:

DirectoryIndex new-index.html

Prevent directory listing

By default, most web servers display a list of all files contained in a directory if no default index page is found in that directory. However, this is generally not desired as users would then be able to see all the files, some of which may not be really public. So, for directories without an index page, you can add the following code to the htaccess file in that directory to prevent it:

Options All -Indexes

Eric Geier is a freelance technical writer. Follow his writings on Facebook. He is also the founder of NoWiresSecurity, a cloud-based Wi-Fi security service, and On Spot Techs, an RF site survey company and other IT services.

Follow ServerWatch on Twitter and on Facebook

Source link


Leave A Reply