Redirect requests only if the file is not found?中的答案无法解决特定情况,已定义的实际路径和路径(将使用
PATH_INFO
)。
我有.htaccess
在path_info
文件中添加index.php
:
RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(?!index\.php/.*)([a-zA-Z0-9\-\/.]+)$ index.php/$1 [QSA,L]
这与index.php
但我当时想使用 3rdparty ,我使用了以下代码:
RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(?!(3rdparty|index\.php)/.*)(.*)$ 3rdparty/$2 [QSA,L]
此代码的含义是“3rdparty”中的任何文件或文件夹都会覆盖路由,例如。如果访问http://localhost/folder1/
它将显示文件/var/www/3rdparty/folder1/
的内容,但如果该文件在3rdparty
文件夹中不存在,则它将使用系统路由。
这是结构的一个例子:
project
├── index.php
├── .htaccess
└── 3rdparty
├── folder1
└── folder2
├── file1.html
└── file2.html
我想使用其他PHP文件而无需访问http://localhost/3rdparty/something...
示例(请参阅文件夹结构):
http://example/project/folder1
显示此地址http://example/project/3rdparty/folder1
http://example/project/folder2
显示此地址http://example/project/3rdparty/folder2/
http://example/project/folder2/file1.html
显示此地址http://example/project/3rdparty/folder2/file1.html
http://example/project/folder2/file2.html
显示此地址http://example/project/3rdparty/folder2/file2.html
http://example/project/folder3/file3.html
(第3方中没有现有文件)显示此地址的内容http://example/project/index.php/folder3/file3.html
问题是我无法同时使用两者,我该怎么做?
答案 0 :(得分:1)
关于我所说的关于检查它是否存在以及Alejandro发布的链接的内容,这里是适应您的情况。看看这是否适合您。
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} [A-Z]{3,}\ /3rdparty/([^&\ ]+)
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [L]
RewriteCond %{DOCUMENT_ROOT}/3rdparty/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/3rdparty/$1 -d
RewriteRule ^(.+)$ /3rdparty/$1 [L]
RewriteRule (.*) /index.php [QSA,L]
答案 1 :(得分:0)
问题:
RewriteCond %(REQUEST_FILENAME)
。$1
,但右边是$2
或$3
(取决于订单)固定代码(阅读评论):
<IfModule mod_rewrite.c>
RewriteEngine On
# Replace next line by / or by a folder name
# Example: /laravel (http://localhost/laravel)
# Example: /cakephp (http://localhost/cakephp)
# I used /project (http://localhost/project)
RewriteBase /project
# Next line allow access to static files
# no needs type "public" in address, example:
# http://localhost/project/css/file.css
# http://localhost/project/js/file.js
# http://localhost/project/images/file.jpg
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If the address is not equal to
# localhost/css or localhost/js redirect to 3rdparty:
RewriteRule ^(?!(index\.php|public|3rdparty)/.*)(.*)$ 3rdparty/$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If no such file/folder in 3rdparty when use route system in index.php:
RewriteRule ^(?!index\.php/.*)3rdparty/([a-zA-Z0-9\/\-.]+)$ index.php/$1 [QSA,L]
</IfModule>
注意:已移除
\w
示例,因为它允许_
(下划线)