Redirecting a domain to a subfolder without changing the URL, but not allowing otherwise?

Asked

Viewed 1,865 times

3

I have the following problem, I have an application with the following structure:

  • app - Folder to store project files as controllers and models.
  • public_html - Stores public documents on the web, such as index, uploads, css.
  • Routes.php - Manage my project’s routes, called by index.php in the public_html folder
  • .htaccess - "the problem"

I need that when a user accesses exemplo.com, request is redirected to my folder public_html, in case the index.php of that folder would be triggered, and would include the routes.php directory at the level above, but do not want the url exemplo.com/public_html/index.php.

Nor would I like it to be possible to access the url directly as exemplo.com/public_html, i.e., to avoid duplicated Urls as exemplo.com/index.php?pagina=1 and exemplo.com/public_html/index.php?pagina=1, wanted that when someone performs direct access to this page, he be redirected to exemplo.com.

Remarks: I use the Apache server in the Debian 8 distribution and want to make these settings through the .htaccess, have the mod_rewrite active and functional.

I didn’t quote any file from public_html, but I believe that maybe I will need settings in the .htaccess from there.

I plan to implement friendly Urls as well.

Could someone help me solve this problem?

1 answer

3


I got the solution to my problem by merging several tips I found on the internet.

RewriteEngine On

# Redireciona as requisições externas na public_html para a raiz
RewriteCond %{THE_REQUEST} \ /+public_html/
RewriteRule ^ / [L,R=301]

# Internamente reescreve a requisição de volta para a public_html
RewriteRule ^(/)?$ public_html/index.php [L] 
  • Ps: This does not solve the problem with friendly Urls, only solves the access to public_html folder.

Browser other questions tagged

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