regex - 在子文件夹中重写 .htaccess

标签 regex apache .htaccess redirect mod-rewrite

我需要使用 .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/

相关文章:

security - 当您决定返回 403 时始终返回 404

php - 如何在子文件夹中设置索引页

java - 使用正则表达式捕获非连续文本。我该怎么做?

正则表达式匹配包含西里尔字母符号的任何字符串,但标有//、///、///等的注释除外

c# - 正则表达式从 url 获取页码

php - 如何从 URL 中删除这种类型的字符串 #VTkfzCGqqkow

python - 斜杠在python字符串和正则表达式中的使用

apache - Websocket 连接失败 : connection closed before receiving a handshake response

apache - .htaccess 动态授予访问权限(包括系统范围的文件)

python - mod_wsgi 不支持 WSGIPythonHome