user1257255
user1257255

Reputation: 1171

.htaccess: Transfer name to index.php if not directory public

I have this piece of code:

Options FollowSymLinks
<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule> 
<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

I don't know how to allow only directory with name "public" which is inside folder where is .htaccess, other names than this dir should be transfered to index.php. How could I do that?

Upvotes: 1

Views: 87

Answers (2)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Add another RewriteCond to exclude public directory

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^/public(/.*)?$
RewriteRule ^(.*)$ index.php?url=$1 [L]

Now, the redirection would only work for /public directories only. If you want the url to just have the rest of the path that's below public like url=subfolder/page.php for /public/subfolder/page.php use

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^public/?(.*)$ index.php?url=$1 [L]

Upvotes: 1

anubhava
anubhava

Reputation: 784998

Change your code with this:

Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule (?!^public(/.*|)$)^(.*)$ index.php?url=$1 [L,NC,R=302]

    ErrorDocument 404 /index.php
</IfModule>

Upvotes: 0

Related Questions