• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / apache server tutorial (install,config, performance tuning, security, error handling) / htaccess tutorial tips for wordpress #redirect #leverage browser cache #Gzip #KeepAlive / htaccess redirect rules with examples including rewrite rules

htaccess redirect rules with examples including rewrite rules

Table of Contents

Toggle
    • What is .htaccess?
    • Basic Redirect Syntax
    • Common Redirect Scenarios with Examples
      • 1. Redirect a Single Page
      • 2. Redirect an Entire Domain
      • 3. Redirect to HTTPS
      • 4. Redirect www to non-www
      • 5. Redirect Non-WWW to WWW
      • 6. Redirect a Directory
      • 7. Redirect URL with Query Strings
      • 8. Redirect Based on User-Agent
    • Best Practices
  • htaccess redirect subfolder to main domain

What is .htaccess?

The .htaccess file is a powerful configuration file used by Apache web servers. It allows you to define rules for URL redirection, access control, and other server configurations.

Basic Redirect Syntax

apache
Redirect [status] [URL-path] [destination URL]
  • [status]: The HTTP status code (e.g., 301 for permanent redirects, 302 for temporary redirects).
  • [URL-path]: The path of the URL to redirect.
  • [destination URL]: The target URL where visitors will be redirected.

Common Redirect Scenarios with Examples

1. Redirect a Single Page

Redirect one page to another.

apache
Redirect 301 /old-page.html https://example.com/new-page.html
  • When users visit /old-page.html, they will be redirected permanently to https://example.com/new-page.html.

2. Redirect an Entire Domain

Redirect an old domain to a new domain.

apache
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
  • Redirects all traffic from olddomain.com to newdomain.com.

3. Redirect to HTTPS

Force all traffic to use HTTPS.

apache
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  • Ensures that all HTTP requests are redirected to their HTTPS counterparts.

4. Redirect www to non-www

Remove the www prefix from URLs.

apache
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
  • Redirects www.example.com to example.com.

5. Redirect Non-WWW to WWW

Force URLs to use the www prefix.

apache
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
  • Redirects example.com to www.example.com.

6. Redirect a Directory

Redirect all traffic from one directory to another.

apache
Redirect 301 /old-directory https://example.com/new-directory
  • Redirects /old-directory and all its content to /new-directory.

7. Redirect URL with Query Strings

Preserve query strings while redirecting.

apache
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=123$
RewriteRule ^old-page\.html$ https://example.com/new-page.html? [L,R=301]
  • Redirects old-page.html?id=123 to new-page.html and clears the query string.

8. Redirect Based on User-Agent

Redirect users based on their browser or device.

apache
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Mobile [NC]
RewriteRule ^ https://m.example.com%{REQUEST_URI} [L,R=302]
  • Redirects mobile users to a mobile-friendly subdomain.

Best Practices

  • Use 301 for permanent redirects to improve SEO rankings.
  • Use 302 for temporary redirects when testing or for short-term changes.
  • Test your .htaccess rules on a staging server before deploying to production.
  • Always back up your .htaccess file before making changes.

htaccess redirect subfolder to main domain

 

here is wordpress permalink change problem

https://nursejobalert.com/p/rsmssb-nurse-paramedical/

 

https://nursejobalert.com/rsmssb-nurse-paramedical/

 

or

https://nursejobalert.com/p/   all posts  to  https://nursejobalert.com/

 

To set an invisible redirect from root to subfolder, You can use the following RewriteRule in /root/.htaccess :

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ /subfolder/$1 [NC,L]

The rule above will internally redirect the browser from :

  • http://example.com/

to

  • http://example.com/subfolder

And

  • http://example.com/foo

to

  • http://example.com/subfolder/foo

while the browser will stay on the root folder.

 

Another alternative if you want to rewrite the URL and hide the original URL:

RewriteEngine on
RewriteRule ^(.*)$ /store/$1 [L]

With this, if you for example type http://www.example.com/product.php?id=4, it will transparently open the file at http://www.example.com/store/product.php?id=4 but without showing to the user the full url.

 

Primary Sidebar

apache server tutorial (install,config, performance tuning, security, error handling)

  • what is If-Modified-Since HTTP header how to add to Wordpress website
  • htaccess tutorial tips for wordpress #redirect #leverage browser cache #Gzip #KeepAlive
    • htaccess redirect rules with examples including rewrite rules
  • apache modules list (explained, enable disable)
  • connect to server by SSH client Putty SFTP client winscp LAMP, LEMP nginx apache etc linux
  • Lamp stack install on Ubuntu 20.4 LTS apache, mysql, php 7.4(Debian 9 & Ubuntu 18.04 lts)
  • apache performance tuning mpm-worker vs prefork vs event modules
  • enable caching in apache server (mod cache disk cache) vs fastcgi cache
  • how to enable KeepAlive connections in apache nginx wordpress
  • Cache control Header & expires headers How to set Properly
  • apache2 conf sample explained apache configuration file modules etc
  • restart apache in ubuntu (start | stop | reload | config test) centos debian mac linux windows 10 xampp
  • uninstall apache2 ubuntu in 3 steps Debian

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

© 2025 - All Rights Reserved Disclaimer & Privacy Policy