I used to host my websites with third-party hosting service and anchoring links was always easy. Then I decided to set my VPS. Everything is working well, besides the anchor links.
I found out that Apache, by default, doesn't allow me to use some symbols when accessing URLs, but also that is possible to use mod_rewrite to solve that. According to Apache's documentation, whenever I can choose between adding rewrite rules via <Directory>
instead of using .htaccess
files, I should do that. Also, to enable the rewrite engine, RewriteEngine On
and Options FollowSymLinks
must be enabled. Considering that I'm already using RewriteRules to force access through HTTPS (and this is working), I believe my system is ready to receive RewriteRules.
Therefore, I added the rewrite rule to <Directory>
but it's not reaching the expected result, which is instead of accessing mydomain.com/pageA/sectionB
, access the anchor mydomain.com/pageA#sectionB
. Maybe I'm adding the RewriteRule to the wrong place since I found in Apache's documentation this rule to do exactly what I expect. Am I doing something wrong? Follow my ".conf" file.
<VirtualHost *:80>
Servername mydomain.com
ServerAlias www.mydomain.com
RewriteEngine on
RewriteCond %(SERVER_NAME) =mydomain.com [OR]
RewriteCond %(SERVER_NAME) =www.mydomain.com
RewriteRule https://%{SERVER_NAME}%{REQUEST_URI} [END=301,L]
</VirtualHost>
<VirtualHost _default_:443>
ServerAdmin [email protected]
DocumentRoot /var/www/html/mydomain.com/
ServerName mydomain.com
ServerAlias www.mydomain.com
<Directory /var/www/html/mydomain.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride ALL
Order allow,deny
Allow from all
RewriteEngine on
RewriteRule "^/pageA/sectionB/" "/pageA#sectionB/" [NE,R]
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/ssl/certs/my_certificate.cer
SSLCertificatekeyFile /etc/ssl/private/my_key.key
</VirtualHost>
This directive won't match when used in a directory context (ie. inside a
<Directory>
container or.htaccess
), because of the slash prefix on the URL-path you are trying to match. In a directory context, the directory-prefix (which always ends with a slash) is first removed from the URL-path that theRewriteRule
directive matches against, so the URL-path never starts with a slash.So, you basically, just need to remove the slash prefix:
I've also removed the quotes around the arguments (these are only strictly necessary if the argument contains unescaped spaces, otherwise it's just a bit harder to read IMO). It's good practise to always include the
L
flag on external redirects - although this is currently the last rule, so it doesn't really matter (except if you add more rules later).OR, you move this rule to directly inside the
<VirtualHost>
container (ie. a virtualhost context). In this case theRewriteRule
directive matches against the root-relative URL-path which includes the slash prefix.This would actually be preferable in this instance since any
.htaccess
mod_rewrite directives will override the<Directory>
container and yourRewriteRule
directive in the<Directory>
container won't be processed (unless mod_rewrite inheritance is enabled - it's disabled by default).Since you have set
AllowOverride ALL
in the<Directory>
container it is assumed you do have (or at least potential to have) an.htaccess
file that could possibly contain mod_rewrite directives. If you disable.htaccess
overrides (ie.AllowOverride None
) then it doesn't matter.Aside:
This (HTTP to HTTPS redirect) is entirely incorrect/malformed. You are missing the
RewriteRule
pattern (first) argument and the flags argument is malformed. They should read[R=301,L]
.However, you don't need to use mod_rewrite here, when used inside a
<VirtualHost *:80>
container a simple mod_aliasRedirect
directive is sufficient and preferred.Unless you are also implementing HSTS, you should also redirect to the canonical hostname:
For example: