1

I'm now working in a server environment where I can access and edit httpd.conf, which is preferable from a performance and a revision control standpoint. I have a few sites (they are Drupal) running in subdirectories along the lines of example.com/yourname, example.com/anothername, example.com/anotherdev. Right now they have a rewrite rule along the lines of

RewriteBase /yourname

in each of their .htaccess files. This doesn't work in httpd.conf and some of the documentation I've been reading says RewriteBase is bad to put in httpd.conf anyway. Any insight into the right approach would be greatly appreciated.

1

When you don't have a rewrite base, all you have to do is insert what would have been the base into each of your rewrite rules.

RewriteRule ^index\.html$  welcome.html 

Becomes

RewriteRule ^/index\.html$  /welcome.html 
5
  • Which RewriteRules do I use to replace RewriteBase?
    – Melissa
    Sep 11 '14 at 22:39
  • You don't "replace" RewriteBase, you remove it, and then edit each end every one of your RewriteRules to add the slash in as I have done in an example. Sep 11 '14 at 22:44
  • There weren't any rewriterules used, just the rewritebase
    – Melissa
    Sep 11 '14 at 22:54
  • There was an alias for the directory (for example yourname site was in the directory yourname123...I didn't set this server up, so I have no idea why) and when I removed that and then just renamed the subdirectories to their respective names (renamed yourname123 to yourname), it started to work without the rewritebase. I still am curious how to do it with the alias intact though.
    – Melissa
    Sep 11 '14 at 23:05
  • 2
    If there are no rewrite rules, the RewriteBase won't have any effect on its own. It can safely be removed. Sep 11 '14 at 23:36
0

This doesn't work in httpd.conf and some of the documentation I've been reading says RewriteBase is bad to put in httpd.conf anyway.

As you say, this simply "does not work" - it is wholly invalid (not that it's a "bad practise"). The server will fail to start if you try to use RewriteBase in a server or virtualhost context (ie. directly in the server config).

When using mod_rewrite in .htaccess (a directory context) all relative URL-path substitutions are relative to the current directory - the directory that contains the .htaccess file. The RewriteBase directive allows you to override the URL-path that relative path substitutions are relative to. (In much the same way as the BASE HTML tag sets the base URL-path for relative client-side URLs.)

Likewise, if you use mod_rewrite directives in a <Directory> container (again, a directory context) in the server config then these behave the same as when used in .htaccess and the RewriteBase directive can apply here also. This is the easiest way to migrate directives from .htaccess to the "server config". Simply place the same directives in a corresponding <Directory> container and nothing else to change. (You should also probably disable the use of .htaccess entirely if you do this.)

However, in a server or virtualhost context (which is processed before the directory context directives and before the request has been mapped to the filesystem) all URL-paths should be treated as relative to the document-root and should be expressed as root-relative - starting with a slash.

For example...

In a .htaccess file located at /some-directory/.htaccess (relative to the document-root):

# .htaccess
RewriteBase /yourname
RewriteRule ^url-path-to-match$ substitution-string [L]

The RewriteBase directive only applies to relative substitution-string arguments (ie. that do not start with a slash or scheme+hostname). It does not apply to the url-path-to-match (which is always relative to the current directory in a directory context - no slash prefix).

So, the above would internally rewrite a request from /some-directory/url-path-to-match to /yourname/substitution-string.

If you want to do the same in a server or virtualhost context then you need to remove the RewriteBase directive and explicitly state the root-relative URL-paths:

# Server
RewriteRule ^/some-directory/url-path-to-match$ /yourname/substitution-string [L]

Additional...

Note that removing the RewriteBase directive isn't the only change required when moving directives from .htaccess to the server config. Since the directives are processed before the request is mapped to the file-system any server variables that relate to the file-system, such as REQUEST_FILENAME, are not going to be set yet and you need to use lookaheads (eg. LA-U:REQUEST_FILENAME). (REQUEST_FILENAME contains the same as REQUEST_URI when used in a server context, ie. the root-relative URL-path, not the filesystem path.)

Also, the rewrite engine does not loop (pass-through) as it does in a directory context. This must be explicitly triggered (if required) using the PT flag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.