Multiples RewriteRules for 3rdparty and routes

1

I have a .htaccess file that adds PATH_INFO in the index.php file (to the routing 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 that is in index.php

The problem is that I want to use 3rd party softwares (3rdparty) at the same time as the routing system, so I did this in .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 the "3rdparty" folder rewriting to not present the 3rdparty in the url, but if the file or folder does not exist within 3rdparty then the system should use the routes. >

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

Folder structure

This is just an example

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

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

Examples (see folder structure above):

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

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

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

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

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

How can I do this?

    
asked by anonymous 17.05.2015 / 04:01

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 of the example for SEO issues, since it accepts _ underline, see details below (maybe want to modify accepted character types). >

Individual standards

  • \s House whitespace, \n \ r or \t .
  • \S Deny% of \s: home what is not whitespace, \n \r or \t .
  • \w House letters, digits, or _ .
  • \W Denial of \w
  • \d House digits, from 0 to 9.
  • \D Denial of \d

Anchors

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

p>     

17.05.2015 / 20:20