1

I have Apache httpd server running in my Linux box with SSL enabled using LetsEncrypt. The root directory of html files is /var/www/html. Let me show the structure of /var/www/html below;

 -html
  |_ index.php
  |_ file1.php
  |_ dir1
  |  |_ index.php
  |_ dir2
     |_ index.php

I am able to access the website using https://example.com url. What I am trying to achieve is if the user try to access https://example.com/dir3 (which is not present), the index.php file of root directory should be delivered. Or, if the user try to access https://example.com/dir3/file1.php , the file file1.php of root should be delivered.

I tried editing virtual host in /etc/httpd/conf.d/vhost-le-ssl.conf but is showing file not found. Let me describe vhost-le-ssl.conf below;

<IfModule mod_ssl.c>
<VirtualHost *:443>
  ServerName example.com

  ServerAlias www.example.com

  DocumentRoot /var/www/html

  ServerAdmin info@example.com

  Alias /dir3 "/var/www/html"

  <Directory /var/www/html>
    AllowOverride None
  </Directory>

RewriteEngine on

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule dir3/* /var/www/html [R=301]

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>

How can I fix this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Alfred
  • 21,058
  • 61
  • 167
  • 249
  • Is `dir3` a definitively known dir name or shall it represent *"any, but not dir1 or dir2"*? – dodrg Mar 30 '23 at 09:42
  • hi @dodrg dir3 url pattern is constant and I am not looking for solution "any, but not dir1 or dir2" – Alfred Mar 30 '23 at 11:01

1 Answers1

0

As dir3, the "unwanted" directory, is known, there are two obvious possibilities.

1. SymLink the folder.

To create the symlink

ln -s '.' '/var/www/html/dir3'

will make the folder existent, but shows to the content of /. This has some implications:

  • Recursion: As dir3 will become in itself visible again and enabling URLs like http://example.com/dir3/dir3/dir3/dir3/index.html. Thats rarely desirable.
  • Dependencies: If /index.html contains references that rely on its original position, these might fail, depending on the details.

2. ReWrite Rules

The other possibility is to use rewrite rules.

File: /var/www/html/.htaccess

RewriteEngine On
RewriteBase "/"
RewriteCond "%{REQUEST_URI}" "^(/dir3)(/.*)"
RewriteRule ".*" "%2" [R=301,NC,L]

R=301 tells to signal a permanent redirect:

  • The rewritten location will become visible in the address bar
  • Search engines will learn the new address as the successor of the old one (keeps SEO up) This is usefull when migrating from an old structure to a new one.
Alternative:

If you prefer to hide the redirection from the visitor and the search engines, just omit the R=301,:

File: /var/www/html/.htaccess

RewriteEngine On
RewriteBase "/"
RewriteCond "%{REQUEST_URI}" "^(/dir3)(/.*)"
RewriteRule ".*" "%2" [NC,L]
When server conf is prefered:

If you have the possibility to edit the conf-files of your Apache and prefer to disable the use of .htaccess-files, you can configure your rules there by defining the folder they should apply to:

File: httpd.conf or yourVirtualHost.conf

<Directory /var/www/html>
    RewriteEngine On
    RewriteBase "/"
    RewriteCond "%{REQUEST_URI}" "^(/dir3)(/.*)"
    RewriteRule ".*" "%2" [R=301,NC,L]
</Directory>
dodrg
  • 1,142
  • 2
  • 18