skip to Main Content

I have assets[/admin/assets] directory which I restricted access from browser.
So whenever user requested on [/admin/assets] , I want to rewrite rule to [admin/index.html].

I tried with below setting , but this doesn’t rewrite to expected path but show permission access error with 403.

RewriteRule /assets /index.html [L] 

I have one solution for that by handling ErrorDocument. But I don’t prefer it that way, I want to handle by RewriteRule .

2

Answers


  1. This can be achieved using the mod_rewrite module in Apache web server. You can add the following code in your .htaccess file or server configuration file to rewrite the URL:

    RewriteEngine on
    RewriteCond %{REQUEST_URI} /assets
    RewriteRule ^assets/(.*)$ /admin/index.html [L]

    The RewriteEngine directive turns on the rewriting engine.

    The RewriteCond directive specifies a condition that must be met before the RewriteRule is applied. In this case, the condition checks if the requested URL starts with "/assets".

    The RewriteRule directive specifies the actual URL rewriting. The pattern "^assets/(.)$" matches any URL that starts with "/assets/", and captures the rest of the URL into a group (.). The URL is then rewritten to "/admin/index.html". The [L] flag specifies that this is the last rule to be applied, so no further rewriting should take place.

    Login or Signup to reply.
  2. I set by Directory module with Require All Denied

    You can’t block access and rewrite the request. The <Directory> container will take priority. (But there’s no point blocking the request when you want to rewrite it. You are essentially blocking it by rewriting.)

    Remove the <Directory> block and directly inside the <VirtualHost> container (outside of any <Directory> section) use mod_rewrite:

    RewriteEngine On
    
    RewriteRule ^/admin/assets($|/) /admin/index.html [L]
    

    Any requests to /admin/assets or /admin/assets/<anything> are internally rewritten to /admin/index.html.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search