简体   繁体   中英

Recurring RedirectMatch in htaccess

Disclaimer: I am not very good with mod_rewrite or regex.

I have the following in my root .htaccess:

RedirectMatch 301 ^(.+\.pdf)$ files/$1

What I want to achieve is that any file with the extension .pdf in the root folder will be redirected to the /files subdirectory.

The problem is this matches any .pdf on the site and seems to recur when in a subfolder, eg

mydomain.com/some.pdf becomes mydomain.com/files/some.pdf as expected.

However:

mydomain.com/sub/some.pdf becomes mydomain.com/files/files/files/files/files/files/sub/some.pdf when I don't want to rewrite this url at all. I am assuming it hits an infinite loop but cancels itself out after a number of redirects.

Your help is much appreciated.

RedirectMatch 301 ^([^/]+\\.pdf)$ files/$1

The key here is to use [^/]+ instead of .+ so that you don't get / as part of your pattern. This isolates your pattern to only matching pdf files in your root directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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