Php 如何为与指定文件名不同的请求写入重写条件
我面临着Php 如何为与指定文件名不同的请求写入重写条件,php,.htaccess,Php,.htaccess,我面临着htaccess的问题,我想写一个条件来阻止提供指定的文件 如果请求的文件名存在,只需提供文件名即可: RewriteCond %{REQUEST_FILENAME} -f RewriteRule .? - [L] 但是我如何才能保证一个指定的文件不会被提供呢?我已将此路径用于控制器操作。例如example/do_something.php 是指向文件do\u something.php的路径,但也是指向控制器操作的路径,我希望我的htaccess文件服务于此操作,而不是此文件,但仅此
htaccess
的问题,我想写一个条件来阻止提供指定的文件
如果请求的文件名存在,只需提供文件名即可:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
但是我如何才能保证一个指定的文件不会被提供呢?我已将此路径用于控制器操作。例如example/do_something.php
是指向文件
do\u something.php的路径,但也是指向控制器操作的路径,我希望我的htaccess
文件服务于此操作,而不是此文件,但仅此操作。您可以使用此规则:
RewriteRule ^example/do_something\.php$ - [L,NC]
这将跳过此规则下面的所有规则中的dou\u something.php
。我试过了,正如他在文章中所说的那样
它对我不起作用,但我通过将这些文件重定向到我的前端控制器,成功地解决了这个问题:
RewriteRule ^example/(.+)\.php$ %{ENV:BASE}/app.php [L]
因此,在这种情况下,如果有人输入路径example/do_something.php
,文件将不会被送达,请求将由前端控制器发送
完整示例:
# If the requested filename exists
# And my route path for controller action is the same
# Don't serve file serve action
RewriteRule ^example/(.+)\.php$ %{ENV:BASE}/app.php [L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
所以我只是在重写条件之后为所有低于该条件的规则添加这个。是吗?对不起,我没有得到。你想用这条规则做什么?你能举例说明一下吗。另外,您最好将full.htaccess放在questionthx中寻求帮助,但它不起作用如果您想知道我是如何解决它的,请检查我的答案查看答案中的更新规则,这应该是您的第一条规则。好的,您已将示例
添加到path中,因此它现在适合我的示例,但仍然不起作用。
# If the requested filename exists
# And my route path for controller action is the same
# Don't serve file serve action
RewriteRule ^example/(.+)\.php$ %{ENV:BASE}/app.php [L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]