2.7.1.1.3. Redirects
Important points:
- V site settings you can enable a redirect on HTTP / HTTPS and to an address with or without www... When using them, set up a similar redirect in .htaccess not necessary.
- Redirects to .htaccess will not work for files specified in list of static files.
- The redirect will not work for URLs that contain characters at the beginning of the filename
.ht
, for example.htaccess
or.htpasswd
... Please note that this rule does not apply to file extensions..html
or the like.
Redirect directives must be placed in a file .htaccesslocated in root directory of the site, from which a redirect must be performed.
To another site
From one specific site to another with the preservation of the address
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^(www.)?example1.com [NC] RewriteRule ^(.*) http://www.example2.com/$1 [L,R=301]
Instead example1.com
substitute the site address, from which a redirect should be performed, and instead of http://www.example.com
— on which.
All requests to the site for the same page of another
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} (.*) RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
Instead https://example.com/
substitute the name of another domain, on which a redirect must 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 site requests to a separate page of another site
RewriteEngine On RewriteBase / RewriteRule ^(.*)$ http://example.com/desired_page/ [L,R=permanent]
Instead http://example.com/desired_page/
substitute the page address of another site, to which a redirect must be performed
From a certain page of one site to a certain page of another
RewriteCond %{REQUEST_URI} ^/old/address/$ RewriteRule ^.*$ http://example.com/new/address/? [R=301,L]
Instead example.com
substitute the address of the new site, on which a redirect must be performed. Instead of /old/address/
specify the page, with which you need to redirect, and instead of /new/address/
— to which.
For certain IP addresses to another site
RewriteEngine On RewriteCond %{REMOTE_ADDR} X.X.X.X RewriteRule .* https://example.com/new/address/? [R=301,L]
Instead example.com
substitute the address of the new site, on which a redirect should be performed, and instead of /new/address/
— address of the target file or page. Instead of X.X.X.X
specify the IP address for which you want to perform a redirect. The IP address can be specified according to regular expressions.
To the same site
From all non-existent subdomains to the main
Preliminary necessarily turnon processing requests to non-existent subdomains.
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^(.*).example.com [NC] RewriteRule ^(.*)$ http://example.com/$1 [L,R=permanent]
Instead example.com
substitute the address of your site for which the redirect is configured.
From one file to another file or page on the same site
RewriteRule ^path/to/file.php$ /new/address/ [L,R=301]
Instead path/to/file.php
substitute the file address, from which a redirect should be performed, and instead of /new/address/
- the address of the target file or page.
From one page of the site to another page of the same site
RewriteCond %{REQUEST_URI} ^/old/address/$ RewriteRule ^.*$ http://%{HTTP_HOST}/new/address/ [R=301,L]
Instead /old/address/
substitute the page address, with which a redirect should be performed, and instead of /new/address/
— to which.
www / without www
From www to no www
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www.example.com$ [NC] RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Instead example.com
substitute the address of your site for which the redirect is configured.
From without www to www
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]
Instead example.com
substitute the address of your site for which the redirect is configured.
HTTP/HTTPS
HTTP to HTTPS
RewriteEngine On RewriteBase / RewriteCond %{HTTP:SSL} !=1 [NC] RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
HTTPS to HTTP
RewriteEngine On RewriteBase / RewriteCond %{HTTP:SSL} =1 [NC] RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
HTTP to HTTPS for a specific directory
RewriteEngine On RewriteBase / RewriteCond %{HTTP:SSL} !=1 [NC] RewriteRule ^admin(.*)$ https://%{SERVER_NAME}/admin$1 [L,R]
Instead admin
substitute the name of the directory for which the redirect is configured.
HTTP to HTTPS for everything but 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 /path/to/file.php
substitute the path to the file for which the redirect to HTTPS should not work.
HTTP to HTTPS for everything but a 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 /path/to/dir
substitute the path to the directory for which the redirect to HTTPS should not work.
URL change
Removing 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]
Removing an extension
Attention!
Removing an extension from a URL may affect certain systems that use POST methods to send data to a script. It is important to consider that such a rule can create problems in the operation of the site.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 php
specify the extension you want.
Removing the .html extension if there are files of the same name
For example, for example.com/file.html
will be redirected to example.com/file
, but in fact it will open file.html
.
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
Removing extra 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]