如何同时使用多个 RewriteRules 来使用 3rdparty 和路由?
How to use multiple RewriteRules for use 3rdparty and routes at the same time?
The answers present in Redirect requests only if the file is not found? don't solve to the specific situation, for a defined real path and are routes (which will use 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/ [QSA,L]
这与我在 index.php
中的路线系统完美配合
但是我当时想用3rdparty,我用了下面的代码:
RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(?!(3rdparty|index\.php)/.*)(.*)$ 3rdparty/ [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
(3rdparty 中不存在的文件)显示来自此地址的内容 http://example/project/index.php/folder3/file3.html
问题是我无法同时使用两者,我该怎么做?
我所说的关于检查它是否存在以及 link Alejandro 发布的内容,这里是针对您的情况的改编。看看这是否适合你。
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} [A-Z]{3,}\ /3rdparty/([^&\ ]+)
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/ -f [OR]
RewriteCond %{DOCUMENT_ROOT}/ -d
RewriteRule (.*) - [L]
RewriteCond %{DOCUMENT_ROOT}/3rdparty/ -f [OR]
RewriteCond %{DOCUMENT_ROOT}/3rdparty/ -d
RewriteRule ^(.+)$ /3rdparty/ [L]
RewriteRule (.*) /index.php [QSA,L]
问题:
- 开头加了
RewriteCond %(REQUEST_FILENAME)
,没必要。
- 我用的是
</code>,但是右边是<code>
或者</code>(要看顺序)</li>
</ul>
<p>固定代码(阅读评论):</p>
<pre><code><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// [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/ [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/ [QSA,L]
</IfModule>
Note: Removed \w
of example because it allow _
(underscore)
The answers present in Redirect requests only if the file is not found? don't solve to the specific situation, for a defined real path and are routes (which will use
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/ [QSA,L]
这与我在 index.php
但是我当时想用3rdparty,我用了下面的代码:
RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(?!(3rdparty|index\.php)/.*)(.*)$ 3rdparty/ [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
(3rdparty 中不存在的文件)显示来自此地址的内容http://example/project/index.php/folder3/file3.html
问题是我无法同时使用两者,我该怎么做?
我所说的关于检查它是否存在以及 link Alejandro 发布的内容,这里是针对您的情况的改编。看看这是否适合你。
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} [A-Z]{3,}\ /3rdparty/([^&\ ]+)
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/ -f [OR]
RewriteCond %{DOCUMENT_ROOT}/ -d
RewriteRule (.*) - [L]
RewriteCond %{DOCUMENT_ROOT}/3rdparty/ -f [OR]
RewriteCond %{DOCUMENT_ROOT}/3rdparty/ -d
RewriteRule ^(.+)$ /3rdparty/ [L]
RewriteRule (.*) /index.php [QSA,L]
问题:
- 开头加了
RewriteCond %(REQUEST_FILENAME)
,没必要。 - 我用的是
</code>,但是右边是<code>
或者</code>(要看顺序)</li> </ul> <p>固定代码(阅读评论):</p> <pre><code><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// [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/ [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/ [QSA,L] </IfModule>
Note: Removed
\w
of example because it allow_
(underscore)