2.7.1.1.3. Redirects
Important points:
- In site settings, you can enable a redirect to HTTP/HTTPS and to an address with or without www. When using them, you do not need to configure a similar redirect in .htaccess.
- Redirects in .htaccess will not work for files specified in the list of static files.
- Redirects will not work for URLs whose file names begin with the characters
.ht, such as.htaccessor.htpasswd. Please note that this rule does not apply to file extensions such as.htmlor similar.
Redirect directives should be placed in the .htaccess file located in the site root directory, from which the redirect should be performed.
To another site
From one specific site to another with address preservation
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?example1.com [NC]
RewriteRule ^(.*) http://www.example2.com/$1 [L,R=301]
Instead of example1.com, substitute the address of the site from which the redirect should be performed, and instead of http://www.example2.com, substitute the address to which.
All requests to site to same page of another
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Instead of https://example.com/, substitute the name of other domain to which the redirect should be performed. The result is similar to the previous one, except that the domain name of the site from which the redirection occurs is not checked.
All requests to site to separate page of another site
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://example.com/desired_page/ [L,R=permanent]
Instead of http://example.com/desired_page/, substitute the address of the page on other site to which the redirect should be performed.
From specific page of one site to specific page of another
RewriteCond %{REQUEST_URI} ^/old/address/$
RewriteRule ^.*$ http://example.com/new/address/? [R=301,L]
Instead of example.com, substitute the address of the new site to which the redirect should be performed. Instead of /old/address/, specify the page from which the redirect should be performed, and instead of /new/address/, specify to which.
For specific IP addresses to another site
RewriteEngine On
RewriteCond %{REMOTE_ADDR} X.X.X.X
RewriteRule .* https://example.com/new/address/? [R=301,L]
Instead of example.com, substitute the address of the new site to which the redirect should be performed, and instead of /new/address/, substitute the address of the target file or page. Instead of X.X.X.X, specify the IP address to which the redirect should be performed. The IP address can be specified using regular expressions.
To same site
From all non-existent subdomains to main
Be sure to enable processing requests to non-existent subdomains beforehand.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*).example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=permanent]
Instead of example.com, substitute the address of your site for which the redirect is being configured.
From one file to another file or page on same site
RewriteRule ^path/to/file.php$ /new/address/ [L,R=301]
Instead of path/to/file.php, substitute the address of the file from which the redirect should be performed, and instead of /new/address/, substitute the address of the target file or page.
From one page of site to another page of same site
RewriteCond %{REQUEST_URI} ^/old/address/$
RewriteRule ^.*$ http://%{HTTP_HOST}/new/address/ [R=301,L]
Instead of /old/address/, substitute the address of the page from which the redirect should be performed, and instead of /new/address/, substitute the address to which.
www / no www
From www to non-www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Instead of example.com, substitute the address of your site for which the redirect is being configured.
From non-www to www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
Instead of example.com, substitute the address of your site for which the redirect is being configured.
HTTP/HTTPS
From HTTP to HTTPS
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:SSL} !=1 [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
From HTTPS to HTTP
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:SSL} =1 [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
From HTTP to HTTPS for specific directory
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:SSL} !=1 [NC]
RewriteRule ^admin(.*)$ https://%{SERVER_NAME}/admin$1 [L,R]
Instead of admin, substitute the name of the directory for which the redirect is being configured.
From HTTP to HTTPS for everything except one file
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:SSL} !=1 [NC]
RewriteCond %{THE_REQUEST} !/path/to/file.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Instead of /path/to/file.php, substitute the path to the file for which the redirect to HTTPS should not be triggered.
From HTTP to HTTPS for everything except specific directory
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:SSL} !=1 [NC]
RewriteCond %{REQUEST_URI} !^/path/to/dir [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Instead of /path/to/dir, substitute the path to the directory for which the redirect to HTTPS should not be triggered.
Change URL
Remove index.php
php, you can specify any other file type that needs to be removed from the address, for example, html.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
Remove extension
Attention!
Removing the extension from the URL may negatively affect the operation of certain systems that use POST methods to send data to the script. It is important to note that such a rule may cause problems with the site's operation.RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L,QSA]
RewriteCond %{THE_REQUEST} ([^\s]*)\.php(\?[^\s]*)?
RewriteRule (.*) %1 [R=301,L]
Instead of php, specify the desired extension.
Remove .html extension if files with same name exist
For example, for example.com/file.html, a redirect will be performed to example.com/file, but in fact, file.html will be opened.
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html
Remove unnecessary slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} //
RewriteCond %{QUERY_STRING} !http(s|)://
RewriteRule .* /$0 [R=301,L]