1

我想让我的网址漂亮。

example.com/en/directions.php

example.com/en/directions/

都应该重写为

example.com/en/directions

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
</IfModule>

这目前会产生一个 500 错误,example.com/en/directions/并将 .php 保留为example.com/en/directions.php.

如果你能让我理解重写条件和重写规则是如何工作的,那就加分。如果这些条件中的任何一个匹配,它是否遵循然后执行此操作。那么-dand-f呢,他们在做什么呢?

4

1 回答 1

0

The check against -f fails because of the trailing slash so you need to do an additional check against it without a possible trailing slash:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^(.*?)/?$
    RewriteCond %{DOCUMENT_ROOT}/%1.php -f
    RewriteRule ^(.*?)/?$ $1.php
</IfModule>

how rewrite conditions and rewrite rules work. Does it follow if any of these conditions match then do this. And what about the -d and -f, what are they doing?

Any number of conditions preceding a rule only apply to that rule and they are all required to match. If you want one condition or another, then you can include the [OR] flag at the end of the condition:

    RewriteCond %{REQUEST_FILENAME} !-d [OR]
    RewriteCond %{REQUEST_FILENAME} !-f

As for the -f and -d, they are resource checks, to see if a given path points to a file or a directory. See apache's mod_rewrite docs for the full explanation.

于 2012-08-23T18:58:12.980 回答