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
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.
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:Any requests to
/admin/assets
or/admin/assets/<anything>
are internally rewritten to/admin/index.html
.