Registered a rule

RewriteRule ^(.*)/new.php$ $1/new [R=301,L] 

In accordance with the rules, the first part of the record indicates the url section, which should be replaced by the record from the second part. But such a record stubbornly does not want to work.

But another works, in which everything is the opposite:

 RewriteRule ^new$ new.php 

Based on this, we can conclude that mod_rewrite works like this: in the first part, the alias is written (all real references in the code are replaced with it), and in the second part, the real file is accessible under this alias. Does everything really work that way, or am I mistaken somewhere? And if so, then how to make a redirect to the pseudoaddress, when the real name of the file is directly entered into the string?

  • To begin with, give an example - redirect from where? - andreymal
  • Internal redirect, so that when you enter the name of the real file name, an alias is specified in the address bar (/ new, instead of /new.php). That to one file there was only one link. - Swift

1 answer 1

An example of a translation from http to https:

 RewriteCond %{HTTPS} !=on RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L] 

Example of website translation from www to version without www

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

An example of CNC links with 2 character localization language at the beginning:

 RewriteRule ^([az]{2})?/?contacts/?$ index.php?lng=$1&module=contacts [L,QSA]