在子文件夹中重写.htaccess

Nij*_*ijn 14 .htaccess mod-rewrite redirect

我需要使用.htaccess文件在子文件夹中执行重写.当前网址的格式如下:

domain.com/subfolder/chapter.php?urlkey=name
Run Code Online (Sandbox Code Playgroud)

它需要重写为:

domain.com/subfolder/chapter/name
Run Code Online (Sandbox Code Playgroud)

在子文件夹中,我有一个.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>
Run Code Online (Sandbox Code Playgroud)

Modrewrite已启用,但出于某种原因,当我转到网址时

domain.com/subfolder/chapter/name
Run Code Online (Sandbox Code Playgroud)

它返回以下错误:

Notice: Undefined index: urlkey
Run Code Online (Sandbox Code Playgroud)

anu*_*ava 7

将此代码包含在subfolder/.htaccess:

Options -MultiViews
RewriteEngine On
RewriteBase /subfolder/

RewriteRule ^chapter/([\w-]+)/?$ chapter.php?urlkey=$1 [L,NC,QSA]
Run Code Online (Sandbox Code Playgroud)

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

也没有必要重写chapter?...你可以重写chapter.php?...