Search code examples
.htaccessdirectorymasking

.htaccess masking directory with or without slash


I'm trying to mask a directory with some static html content, the simple example would be like this: RewriteRule ^something/?$ sub_directory/something/

Now, when the user follows a ulr like

www.example.com/something/

everything works fine, the static page loads from sub_directory/something with css and images. But when the user follows an url like this: www.example.com/something (without trailing slash), the application returns 404 error.

What am I doing wrong here?

I tried:

RewriteRule ^something(.*) sub_directory/something/$1

But this way, only html content is loaded without any js, css or image files.


Solution

  • Have your rule as this:

    RewriteEngine On
    
    RewriteRule ^(?:js|css|images)/.+$ sub_directory/something/$0 [L,NC]
    
    RewriteRule ^something(?:/.*)?$ sub_directory/$0 [L,NC]
    

    And add this just below <head> section of your page's HTML:

    <base href="/sub_directory/something/" />
    

    so that every relative URL is resolved from that base URL and not from the current page's URL.