问题描述
我有一个名为 dollars 的目录,其中包含一个文件 index.php.我想要 url http://localhost/dollars/foo 进行翻译dollars/index.php?dollars=foo.这是我目前在 dollars 目录中的 .htaccess 文件:
I have a directory named dollars that contains a file index.php. I would like for the url http://localhost/dollars/foo to translate to dollars/index.php?dollars=foo. This is the .htaccess file that I currently have in the dollars directorty:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^index\.php
RewriteRule ^(.+)$ index.php?dollars=$1 [L]
这个想法是除了对 index.php 的请求之外的任何请求都应该使用 RewriteRule.
The idea being that any request other than a request to index.php should use the RewriteRule.
然而,这不起作用.
我一直在寻找一段时间,试图弄清楚如何创建我想要的重定向,但我什至不知道我是否在正确的轨道上.正则表达式从来都不是我的菜.感谢您的帮助!
I've been looking for a while trying to figure out how to create the redirect I want, but I don't even know if I'm on the right track. Regex were never my thing. Thanks for any help!
推荐答案
一个经常使用的重写解决方案是检查被请求的路径是否没有指向实际的文件/目录,如果不是,则重写它't - 因为重写的 URL 将指向一个实际的文件,所以不会发生循环.
A often-used solution for rewrites is to just check that the path being requested doesn't point to an actual file/directory, and rewrite it if it doesn't - since the rewritten URL will then point to an actual file, no loop occurs.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
这篇关于.htaccess 重写规则防止无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!