.htaccess mod_rewrite not working in subdirectory

2019-07-05 00:37发布

问题:

I am trying to use htaccess to rewrite all requests to main.php in a subfolder on my domain. The code in my .htaccess is the following:

RewriteEngine on
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . main.php [L]

I am sure the module is enabled, I have checked using the apache2ctl -M command

root@vps847:/etc/apache2/sites-available# apache2ctl -M
Loaded Modules:
 core_module (static)
 log_config_module (static)
 logio_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 alias_module (shared)
 auth_basic_module (shared)
 authn_file_module (shared)
 authz_default_module (shared)
 authz_groupfile_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cgi_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 php5_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 ssl_module (shared)
 status_module (shared)
Syntax OK

In my default site I have added/edited the following lines:

<Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
</Directory>
<Directory /var/www/public>
        AllowOverride All
</Directory>

I have restarted apache every time after editing something. Yet, everty time I surf to the public directory I see the apache directory listing. I have already tried to add a .htaccess to the root of my apache, but it didn't work either. When I add an error to my .htaccess file I do get an internal server error though.

Can someone find what is wrong with my configuration? I am using Ubuntu server 12.04.

回答1:

Have this inside /public/.htaccess:

DirectoryIndex main.php

RewriteEngine on
RewriteBase /public/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . main.php [L]


回答2:

I was having this issue, and found the working solution to be:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /tools/admin

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /tools/admin/index.php [L]

Hope this helps someone