强制使用.htaccess将文本附加到文件名的末尾

时间:2018-11-28 16:34:20

标签: .htaccess

当前我有此目录:

  • example.com/curriculums/34-william.pdf
  • example.com/curriculums/168-romina.pdf

34-william.pdf和168.romina.pdf是实际文件,并且确实存在,但是如果使用以下URL进行访问,则是可访问的:

  • example.com/curriculums/34-william-cv.pdf
  • example.com/curriculums/168-romina-cv.pdf

因此:

  • 如果访问者打开example.com/curriculums/34-william.pdf,则应返回404响应代码
  • 如果访问者打开example.com/curriculums/34-william-cv.pdf,则服务器应返回example.com/curriculums/34-william.pdf的内容

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

假设curriculums目录中没有任何.htacecss,则可以在网站根目录.htaccess中使用以下规则:

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^curriculums/[\w-]+(?<!-cv)\.\w+$ $1$2 [L,NC,R=404]

RewriteRule ^(curriculums/[\w-]+)-cv(\.\w+)$ $1$2 [L,NC]