Redirection using .htaccess

Most websites are constantly changing "works in progress" and as one updates, rearranges and reorganizes data, avoiding broken links due to relocated content is a common issue. While there are numerous ways to redirect requests for outdated URLs to the files in their new locations (javascript, META refresh tags, PHP or SSI includes etc) one of the quickest and most effective ways to redirect is using .htaccess directives.

With .htaccess one can either have an individual page forward to another page or even redirect entire directories. The syntax for redirection is as follows:

Redirect /oldfile.ext https://domain.com/newfile.ext

(Redirect - relative path to the file's old location - full URL of the file's new location)

Example 1:

You move a file named "file1.html" from a folder named "abc" into a folder named "xyz" and you wish to automatically redirect users entering the old URL to the new location:

Old URL: https://domain.com/abc/file1.html
New URL: https://domain.com/xyz/file1.html

Redirect /abc/file1.html https://domain.com/xyz/file1.html

Example 2:

You rename an entire directory named "current" to "2002" and you want any links pointing to files in the old /current directory to automatically forward to the file's new location in /2002:

Redirect /current https://domain.com/2002

Things to Note

The Redirect directive supported in .htaccess files is a useful however very basic tool, even more complex/advanced redirection is possible using mod_rewrite.

  • 5 Bu dökümanı faydalı bulan kullanıcılar:
Bu cevap yeterince yardımcı oldu mu?

İlgili diğer dökümanlar

Modifying existing .htaccess files

To modify/add directives to the .htaccess file in your public_html directory using "File...

Preventing bandwidth theft using .htaccess

While all HostRocket plans include plenty of bandwidth (transfer) nobody ever has enough...

How to create a user-friendly URL using .htaccess?

If your website is using a long URL like example.com/files/folder/sitemap.html, you can change it...

Creating custom error pages using .htaccess

One of the many things one can do to make their website a little more professional-looking is to...

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 your new page or...