.htaccess 如何检查 URI +在 RewriteRule 之前文件扩展名不存在
当通过这样的 URL 访问页面时:
/pagename
如何通过 htaccess
/pagename.htm
检查以下文件是否存在,如果不存在则加载 index.php?
这就是我正在处理的内容:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
我尝试过这种方法的变体,但没有运气: RewriteCond %{REQUEST_URI} !/.htm -f
When a page is accessed via an URL like this:
/pagename
How can we check if the following file exists via htaccess
/pagename.htm
And then load index.php if not?
Here's what I'm working with:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I've tried a variation of this but no luck:
RewriteCond %{REQUEST_URI} !/.htm -f
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该对你来说相当优雅。如果找不到该文件,它只会将其传递给index.php。您当然可以修改它使用的变量,我只是将“q”用于通用目的。
另外,请注意,您可以将其压缩为一组 RewriteCond。我在这里出于注释目的对它们进行了扩展。
Should work rather elegantly for you. If it can't find the file, it just passes it on to index.php. You can of course modify what variable it uses, I just used 'q' for generic purposes.
Also, do note that you can condense this down to a single set of RewriteConds. I've expanded them for annotation purposes here.