apache - htaccess - 将所有内容重定向到 index.php,文件夹和特定文件除外
问题描述
我已经和这个斗争了太久了。我的 PHP 站点托管了一个 .htaccess 文件,用于重写和重定向。到目前为止,它一直运行良好。现在我只想添加一个子文件夹,/pp
它里面的所有东西都不会被重定向或重写,基本上它不应该被我的 .htaccess 东西触及。
现在,当我转到 mysite.com/pp/test.php(该文件确实存在)时,它会重定向到 mysite.com/index
这是我目前拥有的:
RewriteEngine On
#remove www for all traffic
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#prevent redirect of submit
RewriteRule ^/?submit$ submit.php [L]
#prevent redirect of paypal
RewriteRule ^/?admin$ admin.php [L]
RewriteRule ^/?testPPButton$ testPPButton.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/pp/.* [NC] // this line is not working for some reason
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Options -Indexes
更新.htaccess:
RewriteEngine On
# skip /pp/* from all rules below
RewriteRule ^pp(/.*)?$ - [L,NC]
#remove www for all traffic
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#prevent redirect of submit
RewriteRule ^/?submit$ submit.php [L]
#prevent redirect of paypal
RewriteRule ^/?admin$ admin.php [L]
RewriteRule ^/?testPPButton$ testPPButton.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Options -Indexes
解决方案
就在下面RewriteEngine On
的行添加这个跳过pp/
和这个文件夹下的所有内容:
RewriteEngine On
# skip /pp/* from all rules below
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+pp[/?\s] [NC]
RewriteRule ^ - [L,NC]
# rest of your rules go below this
推荐阅读
- rest - 从 yaml 规范生成请求/响应模板 json
- java - 对于 Gson 类型,方法 toJson(...) 未定义
- json - 在 Hugo 中用字符串按键查找数据
- ignite - 如何从主节点运行 Apache Ignite 集群的所有节点?
- html - 带动态表 bootstrap4 的固定卡
- crystal-lang - 在 JSON::Any 变量中存储字符串不起作用
- javascript - 执行返回什么;本身在这个函数中,第 52 行?
- python - 为某些列的 csv 文件中的所有值添加一个常量
- visual-studio-code - VS Code with webpack starter kit,加载资源失败:服务器响应状态为 404
- android - Android SQLite 更新行不起作用并且没有显示错误