My url:
www.example.com/about/about_me.html
I want it to look like this and open the about_me.html file
www.example.com/about
I want to search for my domain name followed by the /about and the page that opens is the about_me.html. I want to do this with my htacess file. And another thing is where i place my .htacess file in my server
CodePudding user response:
This is made more complicated because /about
maps to a physical directory on the filesystem. Ordinarily, mod_dir will append the trailing slash (with a 301 redirect) in order to "fix" the URL. To prevent this we need to disable this behaviour, however, there are potential caveats and security implications with doing this. If this is disabled, you may need to manually override this elsewhere on the system.
Try the following in the root .htaccess
file:
# Disable directory listings (mod_autoindex)
Options -Indexes
# Prevent mod_dir appending the trailing slash
DirectorySlash Off
# Enable the rewrite engine (mod_rewrite)
RewriteEngine On
# Internally rewrite "/about" to "/about/about_me.html"
RewriteRule ^about$ about/about_me.html [L]
Now, a request for /about
will serve /about/about_me.html
. But a request for /about/
(with a trailing slash) will result in a 403 Forbidden (assuming there is no DirectoryIndex
document in the /about
subdirectory).
And requesting any other directory without a trailing slash (eg. /subdirectory
) will also result in a 403 Forbidden, regardless of whether there is a DiretcoryIndex
document present in that directory or not.
mod_autoindex needs to be disabled (ie. Options -Indexes
) in order to prevent accidental information disclosure when DirectorySlash Off
is set. See the relevant Apache manual page for DirectorySlash
: https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryslash
To make life easier, avoid using URLs (without a trailing slash) that map directly to filesystem directories. For example, you could store all your content in a /content
subdirectory, then the /about
URL maps to /content/about/about_me.html
instead. You then don't need to disable DirectorySlash
(and it's trivial to block direct access to the /content
if you need to).
For example:
# Enable the rewrite engine (mod_rewrite)
RewriteEngine On
# Internally rewrite "/about" to "/content/about/about_me.html"
RewriteRule ^about$ content/about/about_me.html [L]