简体   繁体   English

如何使用php或htaccess从URL隐藏文件夹

[英]How to hide folders from URL using php or htaccess

I have images and documents located after a series of folders like this: 我有位于一系列文件夹之后的图像和文档,如下所示:

http://domain.tld/library/data/info/history-of-america/hoa1.pdf
http://domain.tld/library/data/info/history-of-america/hoa2.pdf
http://domain.tld/library/data/info/50-moments-in-history/50mih.png

I have used php to redirect shorter links to these long URLs like so: 我使用php将较短的链接重定向到这些长URL,如下所示:

http://domain.tld/15
http://domain.tld/16
http://domain.tld/21

But because I am using the header redirect, when I click these short links they redirect to the files and display the long filepaths again. 但是因为我使用的是标题重定向,所以当我单击这些短链接时,它们将重定向到文件并再次显示长文件路径。

How can I preserve the short links or at least get rid of the folders in the long links when the file is visible on the browser? 当文件在浏览器中可见时,如何保留短链接或至少去除长链接中的文件夹?

http://domain.tld/15
http://domain.tld/16
http://domain.tld/21

OR

http://domain.tld/hoa1.pdf
http://domain.tld/hoa2.pdf
http://domain.tld/50mih.png

Note I have hundreds of these files so manually inputting for each one is not scalable for me. 注意我有数百个这样的文件,因此对于我来说手动输入每个文件都是不可扩展的。

Thanks in advance. 提前致谢。 I've just been getting so much errors when trying other Q&A solutions so a clear path would be much appreciated. 在尝试其他问答解决方案时,我一直遇到很多错误,因此希望您能找到一条清晰的道路。

You can use this code in your DOCUMENT_ROOT/.htaccess file: 您可以在DOCUMENT_ROOT/.htaccess文件中使用以下代码:

RewriteEngine On

RewriteRule ^15/?$ library/data/info/history-of-america/hoa1.pdf [L,NC]

RewriteRule ^16/?$ library/data/info/history-of-america/hoa2.pdf [L,NC]

RewriteRule ^21/?$ library/data/info/50-moments-in-history/50mih.png [L,NC]

Include this in your .htaccess page 将此包含在您的.htaccess页面中

#url forwarded
RewriteRule ^([a-z0-9_.-]+)$ library/data/info/history-of-america/hoa1.pdf$1 [L,NC,QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM