On the website.conf file I have:
<VirtualHost *:80>
DocumentRoot /srv/http/website/cgi-bin
ServerName website
ServerAlias www.website
RewriteEngine on
RewriteRule ^$ ""
RewriteRule ^([a-z]+)$ /?tab=repo
...
My goal is to have http://localhost/ redirect to localhost and http://localhost/word redirect to http://localhost/?tab=word.
With the current directives I get a 404 error, because it’s trying to open the file repo @ DocumentRoot. All I need is to rewrite the URL to make the word be a GET variable.
A directive like the following works:
RewriteRule /word$ http://localhost/?tab=word
This is obviously somewhat simplistic because I would then have to do it for every possibility.
I experimented with those directives on this website https://htaccess.madewithlove.com/, that I found from another thread on SO, the results are what I expect them to be, I.E.: http://localhost/word is transformed to http://localhost/?tab=word.
Extra info: The website does not have any PHP.
2
Answers
After tinkering a bit more I got down to this:
It opens files if they exist and sends requests that don't exist to my C++ cgicc program. The only thing I don't understand is why the
-d
condition isn't opening directories the same way the-f
one opens files.The difference with the "working directive" is that you’ve included a slash prefix. The regex
^([a-z]+)$
does not allow for a slash prefix, so never matches.You are also failing to use the captured backreference (ie.
$1
) in the substitution string, so it would always rewrite to/?tab=repo
regardless of the URL requested.Consequently, the first rule, that matches against
^$
will never match either – but this rule is not required. You are not performing a redirect when requesting the root – you just don’t want to do anything and instead allow mod_dir to serve the directory index.In a virtualhost context the URL-path matched by the
RewriteRule
pattern is a root-relative URL-path, starting with a slash.So, your rule(s) should be like this instead:
(Or, make the slash prefix optional, ie.
^/?([a-z]+)$
)However,
/?tab=<word>
is not strictly a valid end-point. What is the actual file that is handling the request? This should be included in the rewrite (and not rely on theDirectoryIndex
). You state you are not using PHP, so how are you reading the URL parameter?You are not using
.htaccess
in your example. mod_rewrite behaves slightly differently depending on context (.htaccess
, directory, virtualhost and server).Reference: