I have tried using .htaccess to hide two folders from the browser's URL bar, and only have to put the name of the view file
For that use
RewriteRule ^(.*)$ resources/view/ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME}.view.php -f
RewriteRule ^(.*)$ $1.view.php [NC]
The problem is that I don't get it, I treat the view file as if it were a directory of folders:
This is the structure of my project:
Now, what I am looking for is that the URL ignores the resources/view/ folders and that when I put the name of the file in the URL it shows me the view. As in the image that I posted first above, the URL shows that I put "php.devel.com/test/php_tests/blog/profile" that URL should show me the view profile.view.php.
I have also configured .htaccess so that it ignores the extensions of the .view.php or .php files, that is why I only put the profile without the extensions in the URL
This setup works on a virtual server pointing to the
public_html
.Note: By uncommenting the line
LogLevel
you can see in the error log how Apache resolves the rewrites.He
.htaccess
is in thepublic_html
.The first condition is to get
%1
the request uri without the'/'
initial one.The second condition checks for the request uri without the
'/'
initial one exists as a file.view.php
inside the document root of the virtual server in the subfolderresources/view
.If this condition is met, it rewrites the url.
The structure where it was tested:
Invoking
http://test.localhost.com/perfil
displays the content ofresources/view/perfil.php
. Same withindex
.You need to capture the name of the view:
RewriteRule .*/(.*)$ resources/view/$1.view.php [L]
The directive
RewriteCond
is only to know if the rule is evaluated, so it is not always necessary.