Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 拒绝直接访问php文件_Regex_Apache_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

Regex 拒绝直接访问php文件

Regex 拒绝直接访问php文件,regex,apache,.htaccess,mod-rewrite,redirect,Regex,Apache,.htaccess,Mod Rewrite,Redirect,我编写这段代码是为了将所有内容重定向到index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA] 我想以某种方式调整-f允许将.php文件301重定向到/,而无需进入重定向循环。我有/foo/bar/218加载index.php,它需要view/foo\u bar.php。我希望通过地址栏无法访问/view/foo_bar

我编写这段代码是为了将所有内容重定向到index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]

我想以某种方式调整
-f
允许将
.php
文件301重定向到
/
,而无需进入重定向循环。我有
/foo/bar/218
加载
index.php
,它需要
view/foo\u bar.php
。我希望通过地址栏无法访问
/view/foo_bar.php

您可以尝试以下规则:

# redirect .php requests to /
RewriteCond %{THE_REQUEST} \.php[\s?] [NC]
RewriteRule ^ / [L,R=302]

# existing rules without .php files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.php$ index.php [QSA]

谢谢,它很有效。您能告诉我,这部分
[\s?]
是做什么的吗?当然
[\s?]
在正则表达式中被称为字符类,它与
.php
之后的空格或文字
相匹配