apache - 如何使用 htaccess 将斜杠附加到 URL?
问题描述
我想从 重定向author/$username
到author/$username/
。$username
是作者的用户名。
这是我的.htaccess
内容:
RewriteEngine on
RewriteRule ^author/([A-Za-z0-9-\+]+)/?$ author/index.php?username=$1 [NC,L,QSA]
RewriteRule ^authorpost/([A-Za-z0-9-\+]+)/postid/([0-9]+)/?$ author/index.php?username=$1&postid=$2 [NC,L,QSA]
我需要如何更改它?
解决方案
那么,如果我理解正确,您只需要在 URL 上附加一个斜杠/author/<username>
吗?
在文件顶部尝试这样的.htaccess
操作:
RewriteRule ^(author/[A-Za-z0-9+-]+)$ /$1/ [R=302,L]
字符类+
内部没有特殊含义,因此不需要转义。连字符 ( -
) 应位于字符类的开头或结尾,以匹配文字连字符(无需转义)。
在以下重写中,您现在可以强制使用尾部斜杠,而不是使其成为可选。例如:
RewriteRule ^author/([A-Za-z0-9+-]+)/$ author/index.php?username=$1 [NC,L,QSA]
概括
RewriteEngine On
# Append trailing slash if omitted
RewriteRule ^(author/[A-Za-z0-9+-]+)$ /$1/ [NC,R=302,L]
# Internal rewrites...
RewriteRule ^author/([A-Za-z0-9+-]+)/$ author/index.php?username=$1 [NC,L,QSA]
RewriteRule ^authorpost/([A-Za-z0-9+-]+)/postid/([0-9]+)/?$ author/index.php?username=$1&postid=$2 [NC,L,QSA]
理想情况下,您应该避免在内部重写NC
时使用该标志,以避免潜在的重复内容。至少,它应该是不必要的。
推荐阅读
- json - 我的 Json/javascript 轮播没有显示,甚至不会显示。与卡片组相同
- azure-active-directory - Azure 通用凭据:在 get_token_with_client_credentials 中检索访问令牌时连接到 Blob 存储时出错
- javascript - 如何使用 DOM (*object*.style) 更改 iframe 的灰度?
- azure - Azure YAML 构建;在解决方案中只构建单个项目?
- freertos - FreeRTOS 中等待任务自行删除的正确方法是什么?
- json - python写入json文件而不覆盖
- sql - 左连接临时表时,将表达式转换为数据类型 int 时出现算术溢出错误
- jenkins - Flyway 和 Jenkins 集成使用来自 github 路径的位置
- python - 为什么我的自动编码器的潜在空间输出不是我预期的形状?
- performancecounter - 使用自定义性能计数器所需的权限