简体   繁体   中英

Conflicting htaccess rewrite rules

I am using two rewrite rules within a directory - one to remove php extension from all php files and the other for a pretty URL but they seem to be clashing with each other.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule ^view/doc/([a-zA-Z0-9.]+) /documents/view.php?doc=$1 [L]
RewriteRule ^view/photo/([a-zA-Z0-9.]+) /documents/view.php?photo=$1 [L]

When I try and link to a file, the file does not load - when I check the contents of the GET array I can see that the filename has .php added to it so it, correctly in this case, is never viewable.

Array ( [doc] => 31f224f801547fd743d5e935d7050a3a.pdf.php )

The file itself could be any 32 characters followed by a number of extensions (doc, xls, pdf, txt, rtf and more) so I am assuming that the regex is correct so is the first rewrite causing this and, if so, can I use both in the same directory without any issues?

Currently your capture group for the /view/doc and /view/photo routes contains 1+ [a-zA-Z0-9.] characters. This means the .php extension (from your initial redirect) gets included.

I believe you realized this, but I wanted to re-iterate it since the solution is simple. All you should have to do is match the .php outside of the capturing group(s):

RewriteRule ^view/doc/([a-zA-Z0-9.]+)\.php /documents/view.php?doc=$1 [L]
RewriteRule ^view/photo/([a-zA-Z0-9.]+)\.php /documents/view.php?photo=$1 [L]

Demos: old and new

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