Understanding WordPress redirects

How does Wordpress redirect access to database content from URLs?

Wordpress includes URL rewriting code in the .htaccess file on each hosting, which replaces the dynamic URL with a static URL when the webmaster/blogger has selected this option. This is what the server does, but it does more.

.htaccess code

# BEGIN WordPress
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Here's what he means in detail...

# BEGIN WordPress
# END WordPress

Simple comments about the beginning and end of URL rewriting code.

<IfModule mod_rewrite.c>
</IfModule>

Conditional Expression: Code included between these two tags is executed only if the rewwriting URL is allowed by the hoster, in which case the mod_rewrite module should be available.

RewriteEngine On

Enables the URL rewriting mechanism.

RewriteBase /

Explicitly indicates that the root of the site is "/, "which is not the default for most Unix hosting: the basis of the site is the location of files, something like/home/user/www/.

This directive introduces a distinction between PHP and the server: for PHP, the root remains the location of the directory and PHP variables must be used to know the root of the site (for a convenient application, see the Bioloid script ).

Since the base is defined in this way, you can use relative addresses on the pages, for example/mapage.html.

RewriteCond %{REQUEST_FILENAME} !-f

Proposal for a subsequent directive.

It prevents automatic redirection to index.php, which happens by default when the URL matches the actual file.

REQUEST_FILENAME is an Apache variable that represents the actual path in the file system on the server.

% {} This code is a way to assign an Apache variable.

! this symbol forms the negation of the next filter.

-f clarifies that it is a file, and! -f specifies that it should not be a file.

-d clarifies that it is a directory and! -d vice versa.

Therefore, the condition excludes URLs corresponding to the actual file on the site, while:

RewriteCond %{REQUEST_FILENAME} !-d

excludes subdirectories.

RewriteRule . /index.php [L]

The RewriteRule command prompts you to replace the regular expression, in this case the period, with the address/index.php (unless it is the actual file or directory).

A period representing any character or sequence of characters, any URL is redirected to index.php.

[] adds a rule for the server.
L stands for Last, the rule applies last.

Thus, a complete rule redirects all URLs to/index.php if they do not match the actual files or directories.

New rule

The latest version of the ntaccess file contains an additional rule:

RewriteRule ^index\.php$ - [L]  
This prevents/and/index.php for a subdirectory from being treated as the same URL and speeds up scripts .

Explanation of operation

By default, all accesses are redirected to index.php when they are not real files. But index.php is not really a page, it is a script that looks for articles in the database .

Page or file URLs that are on the site but are not managed by WordPress, such as document.html or archive.zip or image.jpg, are served directly.

When you select dynamic URLs https://www.iqlevsha.ru/index.php?p=n a shape, the script searches the database for an entry that matches the parameter ID and builds a page with a template.

It is recommended that you select static URLs from keywords with the added identifier. This is done in variants in the permalian section, with something like:

/%postname%-%post_id% 

Meaningful URLs of type https://www.iqlevsha.ru/mon-article-12345 are also redirected to index.php.
But the "mon-article-12345" part occurs in a database that records URLs when the ticket is created (and built from the name), and then the software takes the appropriate content to compose the page.

Page 404

It is also controlled using software: if the index.php script does not find the requested URL, it loads a special page, 404.php, which is attached to the topic.

If you want to improve URL error management by Internet users, it is better to modify the 404.php file, not the .htaccess file.
See, for example, the article: Smart page 404.

External relations