How to remove public from url

Asked

Viewed 571 times

2

I’m creating a small application and have a url like root/public/index.php, wanted to know how to remove this public and leave the url alone root/index.php using .htaccess. I’ve searched Google and only found examples related to Windows and none worked.

  • Lucas, you created the public folder?

  • Yes, I created the public folder

  • Usually what is left in the public folder is what is exposed on the internet. So your webserver only serves the public, are you running where the application? Apache, IIS.. ?

  • I’m using the apache

2 answers

4


you are correct about the .htaccess ; because it is he who controls the distribution and directory names of apache, the server that will compile your php.

To rewrite urls, you need to use htaccess’s rewrite module in this way:

<IfModule mod_rewrite.c>
  RewriteEngine On      
## diretrizes      
  </IfModule> 

In your example, specifically, you want to change a single URL, in case it would look like this:

 <IfModule mod_rewrite.c>
      RewriteEngine On
RewriteRule ^raiz/public/?([A-Za-z0-9-]+)?$ /raiz/$1 [NC,L]

RewriteRule ^raiz/public/?([A-Za-z0-9-]+)?/$ /raiz/$1 [NC,L]
      </IfModule> 

In short, anything added in the third level of the root/public will be passed to "/root/TERCEIRO_NIVEL, in the code, the $1 variable

Ok? I hope I’ve helped and it’s important to know that . htaccess is very important for generating friendlier Urls as well as troubleshooting problems like this.

Remember that guidelines change paths in general, so you may need to use the absolute path to generate links from or as an example.

I will add a . htaccess that I use a lot in my web apps, I hope to help

<IfModule mod_rewrite.c>
  RewriteEngine On
   ##sempre utilizar https
   RewriteCond %{HTTPS} !=on

   RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
  ## URL amigáveis enviando tudo para "site/site.php e diretórios como variaveis link
   RewriteRule ^$ site/site.php [L]

   RewriteRule ^/?([A-Za-z0-9-]+)?$ /site/site.php?link=$1 [NC,L]

   RewriteRule ^/?([A-Za-z0-9-]+)/?$ /site/site.php?link=$1 [NC,L]

   RewriteRule ^/?([A-Za-z0-9-]+)/?([A-Za-z0-9-]+)/?$ /site/site.php?link=$1&link2=$2 [NC,L]  

   RewriteRule ^/?([A-Za-z0-9-]+)/?([A-Za-z0-9-]+)/?([A-Za-z0-9-]+)/?$ /site/site.php?link=$1&link2=$2&link3=$3 [NC,L]

   RewriteRule ^/?([A-Za-z0-9-]+)/?([A-Za-z0-9-]+)/?([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /site/site.php?link=$1&link2=$2&link3=$3&link4=$4 [NC,L]

  </IfModule>   
## expirar o cache ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
##  ##  
#compactando arquivos
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
</ifmodule>
#End Gzip
# BEGIN Caching
<ifModule mod_headers.c>
<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch "\\.(css)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</filesMatch>
<filesMatch "\\.(xml|txt)$">
Header set Cache-Control "max-age=216000, public, must-revalidate"
</filesMatch>
<filesMatch "\\.(html|htm|php)$">
Header set Cache-Control "max-age=1, private, must-revalidate"
</filesMatch>
</ifModule>
# END Caching

Abs

  • I used your code and ended up not working, but thanks to it I found a code very similar to it that worked.

2

Based on Leonardo’s answer, I found the code below that solves the problem.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^raiz$ [NC,OR] RewriteCond %{HTTP_HOST} ^raiz$
RewriteCond %{REQUEST_URI} !raiz/public/
RewriteRule (.*) /raiz/public/$1 [L]
  • Thanks Lukas, I hope I helped. I will edit my reply with some "power ups" from . htaccess that has helped in the performance of my web apps. Abs

  • Obg for help, I will test this htaccess

Browser other questions tagged

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