为什么 rewrite_rule 仅在文件夹存在时才有效?

why a rewrite_rule works only when the folder exist?

objective就是输入一个url像

https://www.mywebsite/expert/188/name-of-the-expert

和return它以

的形式发送到服务器
expert.php?exp=188

就像用户输入 https://www.mywebsite/expert.php?exp=188

什么不起作用: 像 RewriteRule ^expert-([0-9]*)$ expert.php?exp= [L,NC,QSA]

这样的简单规则

什么有用 我有以下 rewrite_rule 仅当我 物理上 在我的树中创建文件夹 expert/ 时才有效,即 /www/expert/

# FRIENDLY URL FOR EXPERTS PROFILE
Rewriterule ^(.*)expert\/([0-9]*)(\/[a-z0-9\-\']*)?\/?$ expert.php?exp= [L,NC,QSA]

此外,为了使此规则生效,我必须将 <base href="/"> 放在页面 expert.php 中以避免所有链接资源出错:

Failed to load resource: the server responded with a status of 404 ()

服务器是名为 OVH 的共享 Web 托管平台上的 APACHE。

问题的完整代码:

<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks
RewriteBase /

# FRIENDLY URL FOR EXPERTS PROFILE
Rewriterule ^(.*)expert\/([0-9]*)(\/[a-z0-9\-\']*)?\/?$ expert.php?exp= [L,NC,QSA]
</IfModule>

阅读https://www.adayinthelifeof.nl/2012/01/21/apaches-fallbackresource-your-new-htaccess-command/后,我从使用重写规则改为使用Apaches FallbackResource。它更像是 'if page not found, then run this page instead'

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
    FallbackResource /root.php
</Directory>

我发现的唯一一件事是,如果 URL 是针对确实存在的页面 - 那么在我的情况下它会代替您的基础新基础页面 'root.php'。

关闭 MultiViews 像这样:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

# FRIENDLY URL FOR EXPERTS PROFILE
Rewriterule ^/?expert/(\d+)/?$ expert.php?exp= [L,NC,QSA]

选项 MultiViews(参见 http://httpd.apache.org/docs/2.4/content-negotiation.html)由 Apache's content negotiation module 使用,它在 之前 mod_rewrite 运行并使 Apache 服务器匹配文件的扩展名。因此,如果 /file 是 URL 那么 Apache 将提供 /file.html.