0

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).

I have a .htaccess to add path_info in the index.php file:

RewriteEngine On

RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d

RewriteRule ^(?!index\.php/.*)([a-zA-Z0-9\-\/.]+)$ index.php/$1 [QSA,L]

This works perfectly with my route system that is in the index.php

But I want use 3rdparty at the time, I used the following code:

RewriteEngine On

RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d

RewriteRule ^(?!(3rdparty|index\.php)/.*)(.*)$ 3rdparty/$2 [QSA,L]

The idea of this code is that any file or folder inside the "3rdparty" override the routes, eg. If acess http://localhost/folder1/ it will show the contents of the file /var/www/3rdparty/folder1/ but if the file don't exists in 3rdparty folder, then it will use the system routes.

Folder structure

This is an example of structure:

project
├── index.php
├── .htaccess
└── 3rdparty
    ├── folder1
    └── folder2
        ├── file1.html
        └── file2.html

I want to use other PHP files without having to access an address as http://localhost/3rdparty/something...

Examples (see folder structure):

  • http://example/project/folder1 shows contents from this address http://example/project/3rdparty/folder1

  • http://example/project/folder2 shows contents from this address http://example/project/3rdparty/folder2/

  • http://example/project/folder2/file1.html shows contents from this address http://example/project/3rdparty/folder2/file1.html

  • http://example/project/folder2/file2.html shows contents from this address http://example/project/3rdparty/folder2/file2.html

  • http://example/project/folder3/file3.html (no-existing file in 3rdparty) shows contents from this address http://example/project/index.php/folder3/file3.html

The problem is that I'm not able to use both at the same time, how can I do this?

Community
  • 1
  • 1
Protomen
  • 9,471
  • 9
  • 57
  • 124

2 Answers2

1

Going off what the what I was saying about checking if it exists and the link Alejandro posted, here is an adaption for your situation. See if this will work for you.

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]
Panama Jack
  • 24,158
  • 10
  • 63
  • 95
0

Problems:

  • It was adding RewriteCond %(REQUEST_FILENAME) at the beginning unnecessarily.
  • I used $1, but the right was $2 or $3 (it depends on the order)

Fixed code (read comments):

<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>

Note: Removed \w of example because it allow _ (underscore)

Protomen
  • 9,471
  • 9
  • 57
  • 124