0

I configured my apache2 like this before:

<VirtualHost _default_:443>
    ...
    RewriteEngine on
    RewriteCond %{HTTPS}  !=on
    RewriteRule "^/?(.*)" "https://%{SERVER_NAME}/$1"    # HTTPS auto redirect
    RewriteRule "^/example/?$" "/example.json"
    ...

When I access example.com/example or example.com/example/ it does shows the contents of example.com/example.json.

Then I installed another software and it requires to access /software/web for the main webpage, and directories other than web shouldn't be exposed to the public. So I write this:

# DocumentRoot is /var/www/html
<Directory /var/www/html/software/>
    Require all denied
    AllowOverride All
</Directory>

<Directory /var/www/html/software/web/>
    Require all granted
    AllowOverride All
</Directory>

It works by accessing example.com/software/web. I want to rewrite the link to omit the /web part (so I can just type example.com/software), so I tried to add this:

RewriteRule "^/software/?$" "/software/web"

However when I tried to access the shortened URL it give out a 403 error:

You don't have permission to access /software/ on this server.

I tried to add a trailing slash like this:

RewriteRule "^/software/?$" "/software/web/"

Then it says

You don't have permission to access /software/app.php on this server.

I tried several tweaks to the regex but no good. How to make it work? Any help is appreciated.

whc2001
  • 25
  • 1
  • 5

1 Answers1

0

It looks like you have another RewriteRule somewhere in your config that is rewriting /software/ to /software/app.php. Since /software/app.php doesn't match the regex you've written, it never gets applied.

You will probably want to move this rewrite earlier in your config, before the one that adds the app.php. You may also want to add the [L] flag after the rewrite if you don't want any further rewrites to be applied after the /web/ one.

Ladadadada
  • 26,337
  • 7
  • 59
  • 90
  • It seems like there is an `.htaccess` file under `/software/web`, which has some redirection like `RewriteRule .? %{ENV:BASE}/app.php [L]`. I placed my RewriteRules under `/etc/apache2/sites-enabled`, is that the problem? Also I tried to change to `RewriteRule "^/software/?$" "/software/web/app.php" [L]`, but it just jumps to `/` when I access `/software`. It's really confusing and I wonder what happened here. – whc2001 Mar 24 '19 at 22:50
  • Finally after some tinkering I remembered that the RedirectRule has another flag called `PT` before, and after adding it does work, although I didn't use anything like `Alias` or `Redirect`. The working RewriteRule looks like this: `RewriteRule "^/software/?$" "/software/web" [PT]` – whc2001 Mar 24 '19 at 23:05