Hosting PHP 

How to redirect / rewrite urls using .htaccess file

What is .htaccess?

The .htaccess (Hypertext Access) file is an Apache distributed server configuration file. You can use the .htaccess file to set server configurations for a specific directory. .htaccess file is created in order to enable extra features for that subdirectory.

Creating .htaccess:

we have to create .htaccess in the application root path. But if you want only for a specific subpath then you can create in subpath too.

Note: While adding urls, please replace all the example.com domains with your specific domain.

Redirecting all URLs:

The following line redirects all URLs on your site to the new site.

Redirect 301 / https://example.com/

Redirecting single URL:

This can help in creating a redirect between the old URL to new URL.

First path: is old URL you want to redirect

Second path: is where you want your old path needs to be redirected

Redirect oldpath.html newpath.html

Using Regular Expressions

If you want to use a Regular Expression to redirect something, use the RedirectMatch directive

RedirectMatch "^/oldpath\.html/?$" "https://example.com/newpath.php"

Error message redirect:

You can use theĀ ErrorDocument command to redirect 404 pages not fond to your preferred pages

ErrorDocument 404 /404page.html //if you want o redirect to specific page

or

ErrorDocument 404 https://example.com // redirects to public domain

Adding www in URL:

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

Removing www in URL:

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

 

 

Also Read:  How to Integrate Sendinblue Transactional Email

Related posts