Multiple Rewriterules for 3rdparty and routes

Asked

Viewed 112 times

1

I have a file .htaccess that adds PATH_INFO in the index.php file (for the route system):

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 which is in index.php

The problem is I want to use third-party software (3rdparty) at the same time as the route system, so I did this on .htaccess:

RewriteEngine On

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

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

This Rewriterule tries to access files that are in "3rdparty" folder rewriting to not display the 3rdparty url, but if the file or folder does not exist within 3rdparty then the system must use the routes.

Example, if you access http://localhost/folder1/ will show the contents of the file /var/www/3rdparty/folder1/, but if the file does not exist in the folder 3rdparty then you should use the route system.

Folder structure

This is just one example

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

What I want is to access other PHP files without having to type something like http://localhost/3rdparty/something...

Examples (see folder structure above):

  • http://example/project/folder1 will show the content of http://example/project/3rdparty/folder1

  • http://example/project/folder2 will show the content of http://example/project/3rdparty/folder2/

  • http://example/project/folder2/file1.html will show the content of http://example/project/3rdparty/folder2/file1.html

  • http://example/project/folder2/file2.html will show the content of http://example/project/3rdparty/folder2/file2.html

  • http://example/project/folder3/file3.html (url not existing in the 3rdparty folder) will display the content of http://example/project/index.php/folder3/file3.html

How can I do this?

1 answer

0


Follow the comments in the file to understand how it works

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Troque está linha por / ou pelo nome da pasta que estará
    # usando o seu framework e pastas ex.: /laravel (http://localhost/laravel)
    # ou /cakephp (http://localhost/cakephp)
    # no meu caso /project (http://localhost/project)

    RewriteBase /project

    # Está linha irá liberar acesso aos arquivos estáticos sem precisar
    # digitar public no endereço, como por exemplo:
    # 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

    # Se não for em public então direciona para o 3rdparty
    RewriteRule ^(?!(index\.php|public|3rdparty)/.*)(.*)$ 3rdparty/$2 [QSA,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Se não existir nada em 3rdparty então usa o sistema de rotas
    RewriteRule ^(?!index\.php/.*)3rdparty/([A-Za-z0-9\/\-.]+)$ index.php/$1 [QSA,L]
</IfModule>

Note: Removed \w from the example for SEO reasons, because it accepts _ underline, see the following details (if you want to modify the accepted character types).

Individual standards

  • \s Box spaces in white, \n\r or \t.
  • \S Denial of \s: box which is not white space, \n \r or \t.
  • \w Box letters, digits, or _.
  • \W Denial of \w
  • \d Box digits, from 0 to 9.
  • \D Denial of \d

Anchors

  • \b House the separation of words, which also includes the beginning ^ and the end $ of the character string tested. The definition of the characters that form words varies according to the implementation, but it is safe to assume at least [a-zA-Z0-9_]. With support, the shortcut \w is a valid alternative. Java is a notable exception in that it supports \b but not \w. Note that although similar to the word boundaries defined by POSIX, this escape sequence does not distinguish the beginning and end of the word, only the separation itself.
  • \B Denial of \b
  • \A Matches the beginning of the string. In a multi-line situation, does not match the beginning of the following lines, which differs from ^.
  • \Z Box the end of the string or the position just before the line breaks at the end of the string. In a multi-line situation, it does not match the end of the following lines, which differs from $.
  • \z Box the end of the character string.

Source: http://en.wikipedia.org/wiki/Regular_expression

Browser other questions tagged

You are not signed in. Login or sign up in order to post.