This is the .htaccess code in WordPress. Can someo

2019-01-10 17:39发布

问题:

This is the .htaccess code for permalinks in WordPress. I don't understand how this works. Can someone explain?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I googled and found out that -f and -d part means to give real directories and files higher priority.

But then what are ^index\.php$ - [L] and RewriteRule . /index.php [L] ?

How does WordPress process categories, tags, pages, and etc. with just this?

Does it happen internally? If so, I'm interested in learning how to do it in PHP.

Thanks

回答1:

^index\.php$ - [L] prevents requests for index.php from being rewritten, to avoid an unnecessary file system check. If the request is for index.php the directive does nothing - and stops processing rules [L].

This block is all one rule, and it says that if it is not a real file and not a real directory, reroute the request to index.php.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

index.php itself interprets the URL that was requested by the client (PHP can see the requested URL using $_SERVER['REQUEST_URI']) and it calls the correct code for rendering the page the user requested.