如何使用多个RewriteRules同时使用3rdparty和路由?

时间:2015-05-17 02:21:24

标签: php apache .htaccess mod-rewrite url-rewriting

  

Redirect requests only if the file is not found?中的答案无法解决特定情况,已定义的实际路径和路径(将使用PATH_INFO)。

我有.htaccesspath_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

问题是我无法同时使用两者,我该怎么做?

2 个答案:

答案 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示例,因为它允许_(下划线)