htaccess重定向到pdf的路径

问题描述

我有一个名为/manuals的目录,其中包含多个PDF文件

我想要实现的是将pdf扩展名重定向并附加到访问该目录的任何URL,如下所示:

https://website.com/manuals/FM0100 to https://website.com/manuals/FM0100.pdf

.htaccess文件中是否可能?

解决方法

经过实验,我找到了解决方法:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} manuals
RewriteCond %{REQUEST_URI} !pdf
RewriteCond %{REQUEST_URI} !png
RewriteCond %{REQUEST_URI} !jpg
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}.pdf

</IfModule>
,

/manuals/.htaccess文件中(即内部 /manuals子目录),您可以执行以下操作:

RewriteEngine On

# Append ".pdf" to any request that does not already have a file extension.
RewriteRule !\.\w{2,4}$ %{REQUEST_URI}.pdf [R=302,L]

使用302(临时-重定向)外部重定向请求。

或者,您可以通过删除.pdf标志来内部重写请求(即对用户隐藏的R=302扩展名)。

不需要<IfModule>包装器。