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
- [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.
- When users visit
/old-page.html
, they will be redirected permanently tohttps://example.com/new-page.html
.
2. Redirect an Entire Domain
Redirect an old domain to a new domain.
- Redirects all traffic from
olddomain.com
tonewdomain.com
.
3. Redirect to HTTPS
Force all traffic to use HTTPS.
- Ensures that all HTTP requests are redirected to their HTTPS counterparts.
4. Redirect www to non-www
Remove the www
prefix from URLs.
- Redirects
www.example.com
toexample.com
.
5. Redirect Non-WWW to WWW
Force URLs to use the www
prefix.
- Redirects
example.com
towww.example.com
.
6. Redirect a Directory
Redirect all traffic from one directory to another.
- Redirects
/old-directory
and all its content to/new-directory
.
7. Redirect URL with Query Strings
Preserve query strings while redirecting.
- Redirects
old-page.html?id=123
tonew-page.html
and clears the query string.
8. Redirect Based on User-Agent
Redirect users based on their browser or device.
- 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 :
to
And
to
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.