.htaccess重写 - 如果文件存在与参数同名,则失败

时间:2022-10-06 10:08:37

I have been having a little trouble with a rewrite rule which, after Googling around led me nowhere. I have a standard rewrite of a URL setup like this:

我在重写规则方面遇到了一些麻烦,在谷歌搜索后,我无处可去。我有一个标准的URL设置重写,如下所示:

RewriteRule ^services/(.*)/ /services/index.php?content=$1 [L]

The site uses the value of content to request the related content from the DB but just makes the URL look prettier; standard stuff. This works to a certain extent. If, for example, my URL us /services/testimonials/ the rule works fine; the content gets loaded and the page displays normally. What I was seeing with some URLs though (e.g. /services/training/) was an error stating:

该站点使用内容的值来从数据库请求相关内容,但只是使URL看起来更漂亮;标准的东西。这在一定程度上起作用。例如,如果我的网址us / services / testimonials /规则正常;内容被加载,页面正常显示。我通过一些URL看到的内容(例如/ services / training /)是一个错误说明:

Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0 Fatal error: Unknown: Failed opening required 'redirect:/services/index.php' (include_path='[removed]') in Unknown on line 0

After much Googling and head banging (with little progress from either) I discovered that if I changed the URL to /services/Training/ (notice the capitalised T) it worked. This quickly led me to a thought about a conflict in the URL with something (which I probably should have thought of first in hindsight). After checking which URLs worked and which didn't, I connected the dots and found the URLs that were broken had files with the same name in subdirectories the same as the URL. That sentence is confusing. Basically I cannot use the URL /services/training/ as there is a file /services/training.php which causes the return of the error.

经过大量的谷歌搜索和头部敲击(两者都没有什么进展),我发现如果我将URL更改为/ services / Training /(注意大写的T),它就可以了。这很快让我想到了URL中的某些冲突(事后我可能应该首先想到)。检查哪些URL有效,哪些没有,我连接了点,发现被破坏的URL在子目录中具有与URL相同的名称的文件。那句话令人困惑。基本上我不能使用URL / services / training /,因为有一个文件/services/training.php会导致错误的返回。

There is an obvious fix to this which is to rename the files that conflict with the URLs, but is there a way in the rewrite rule where I can get it to ignore any files it may find of the same name?

有一个明显的解决方法是重命名与URL冲突的文件,但在重写规则中是否有一种方法可以让它忽略它可能找到的同名文件?

1 个解决方案

#1


5  

Options -MultiViews

This should prevent the url from mapping to training.php. It will however not prevent /services/training.php from not mapping, but that won't be a problem I presume.

这应该可以防止url映射到training.php。但是它不会阻止/services/training.php不映射,但这不是我认为的问题。

#1


5  

Options -MultiViews

This should prevent the url from mapping to training.php. It will however not prevent /services/training.php from not mapping, but that won't be a problem I presume.

这应该可以防止url映射到training.php。但是它不会阻止/services/training.php不映射,但这不是我认为的问题。