.htaccess 仅在目标不存在时重定向

.htaccess only redirects when target does not exist

我正在尝试使用 .htaccess 作为 url 的别名,例如 example.com/shortexample.com/short.php,但是当我测试配置时,我只会在请求 php 不存在的脚本。例如:页面 short.php 存在,但 shor.php 还不存在,当我请求 example.com/short 的页面时,我得到 example.com/short 的 404 页面,但是当我请求 example.com/shor 我得到 example.com/shor.php

的 404 页面

我用的.htaccess是:

RewriteEngine on
RewriteRule ^s/([A-Za-z0-9-]+)/?$ short.php?code= [NC]
RewriteRule ^([A-Za-z0-9-]+)/?$ .php [NC,L]

在你的 .htaccess 中使用它:

RewriteEngine on
Options -MultiViews

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^s/([A-Za-z0-9-]+)/?$ short.php?code= [NC,L]

RewriteCond %{DOCUMENT_ROOT}/\.php -f [NC]
RewriteRule ^([A-Za-z0-9-]+)/?$ .php [NC,L]