只有在找不到文件的情况下,重定向请求中才会出现答案?对于已定义的真实路径和are路由(将使用
PATH_INFO
(,不要解决特定情况。
我有一个.htaccess
要在index.php
文件中添加path_info
:
RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(?!index.php/.*)([a-zA-Z0-9-/.]+)$ index.php/$1 [QSA,L]
这与我在index.php
中的路线系统完美配合
但我当时想使用第三方,我使用了以下代码:
RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(?!(3rdparty|index.php)/.*)(.*)$ 3rdparty/$2 [QSA,L]
该代码的思想是,"第三方"中的任何文件或文件夹都会覆盖路由,例如,如果访问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
(第三方没有现有文件(显示来自此地址http://example/project/index.php/folder3/file3.html
的内容
问题是我不能同时使用两者,我该怎么做?
根据我所说的检查它是否存在以及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]
问题:
- 它在一开始就添加了不必要的
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
,因为它允许_
(下划线(