stijnb1234
stijnb1234

Reputation: 184

Redirect to public folder, with support for a trailing slash

This is my .htaccess file:

RewriteEngine On

DirectoryIndex index.php

#Set 404 and 505 pages
ErrorDocument 404 https://example.com/404/
ErrorDocument 500 https://example.com/500/

#Ignore AJAX requests
RewriteCond %{REQUEST_METHOD} ^(POST)$
RewriteCond %{HTTP:X-Requested-With} XMLHttpRequest [NC]
RewriteRule ^ - [L]

#Redirect 404 to correct page
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /public/404.php [L]

#Redirect 500 to correct page
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /public/500.php [L]

#Rewrite to /public folder
RewriteRule ^$ public/index.php [L]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

#Hide .php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]

/admin/ also has a .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]

It works fine, except in one situation.

The correct situations:

example.com/page -> Just shows the page
example.com/admin/ -> Works fine. 

But if you enter example.com/admin (without the trailing slash), it redirects to /public/admin/. I want to hide that public from my URL.

How to always get a clean URL, that always hides the /public from the URL.

Upvotes: 1

Views: 621

Answers (1)

anubhava
anubhava

Reputation: 784998

Insert this redirect rule just before #Rewrite to /public folder line to add a trailing slash in the event if /public/admin/ is also a directory:

# add a trailing slash if public/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/public/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE]

Upvotes: 1

Related Questions