我需要使用 .htaccess 文件在子文件夹中执行重写。当前 url 的形式如下:
domain.com/subfolder/chapter.php?urlkey=name
需要改写为:
domain.com/subfolder/chapter/name
在子文件夹中,我有一个带有以下代码的 .htaccess 文件:
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^chapter/([a-z0-9]+)/?$ chapter.php?urlkey=$1 [L,NC,QSA]
</IfModule>
Modrewrite 已启用,但出于某种原因,当我转到 url 时
domain.com/subfolder/chapter/name
它返回以下错误:
Notice: Undefined index: urlkey
最佳答案
在 subfolder/.htaccess
中有此代码:
Options -MultiViews
RewriteEngine On
RewriteBase /subfolder/
RewriteRule ^chapter/([\w-]+)/?$ chapter.php?urlkey=$1 [L,NC,QSA]
选项
MultiViews
(见 http://httpd.apache.org/docs/2.4/content-negotiation.html )被 Apache's content negotiation module
使用运行 之前 mod_rewrite
并使 Apache 服务器匹配文件的扩展名。所以如果 /file
是 Apache 将服务的 URL /file.html
.也不需要重写到
chapter?...
您可以改写为chapter.php?...
关于regex - 在子文件夹中重写 .htaccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38952714/