Home > Enterprise >  Where RewriteBase goes?
Where RewriteBase goes?

Time:02-23

I'm gonna take a code from this answer: Redirect folder to another with htaccess

So, I'd like to know where RewriteBase goes for real?! According to the smart guy from the linked answer it goes to the first part of the RewriteRule. So the second part requires a slash ("/")

Options  FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^test/(.*)$ /new/$1 [L,NC,R=302]

I don't remember where, but I read somewhere that it goes to the second part. And in fact my .htaccess is written in accordance with this rule. So, my rewrite rules looks this way

RewriteRule ^script$ folder/some_script [L,NC,R=302]

And actually it works for me. But now I read the linked answer because I need something other and I'm confused. I have no idea if this affects performance much, I just want to do it right. And I want to know "where it goes for real", to the first part, to the second, or to the both? Now I need a redirect from the folder script to another folder script. So how it should be?

#v1
RewriteBase /
RewriteRule ^/folder/some_script$ other_folder/some_other_script [L,NC,R=302]

#v2
RewriteBase /
RewriteRule ^folder/some_script$ /other_folder/some_other_script [L,NC,R=302]

#v3
RewriteBase /
RewriteRule ^/folder/some_script$ /other_folder/some_other_script [L,NC,R=302]⠀

#v4
RewriteBase /
RewriteRule ^folder/some_script$ other_folder/some_other_script [L,NC,R=302]⠀

I'd like to clarify that "yes", I need a redirect to another folder and which has a script with different name, so I don't need this "$1". One more thing please, don't guess, don't answer if you don't know that for sure. I can guess a lot of things too, I can test if this is work. I know that when I put the slash to the second part, it works, and if you don't put it also works. I mean:

RewriteBase /
# this works for me correctly
RewriteRule ^some_script$ folder/some_script [L,NC,R=302]

# this also works for me correctly, but I use the first way
# RewriteRule ^script$ /folder/some_script [L,NC,R=302]

I want to know how it's right! And I want to know how to do it right with a redirect from a folder.

CodePudding user response:

If the target (the second argument) of a RewriteRule is relative, then the value of RewriteBase is prepended to it.

So, let's take RewriteBase /some/path/ as example.

  • RewriteRule ^foo$ /bar: RewriteBase doesn't apply since the target (/bar) is absolute
  • RewriteRule ^foo$ http://%{HTTP_HOST}/bar: same because it is an HTTP redirect to an URL, still not a relative path
  • RewriteRule ^foo$ bar: RewriteBase applies since bar is relative so the request /foo will be rewritten to /some/path/bar

But, wait, it also works without an explicit RewriteBase? Yes, it can: Apache is able to "guess" the path to prepend by default to a relative target but there is some cases where Apache isn't able to correctly guess it (when an Alias comes to play or the use of VirtualDocumentRoot, UserDir for examples)

In case of a redirect (R=30[12]) with a relative path (instead of an URL), Apache works the same: it will just then prepend the ServerName and appropriate scheme on top of it.

Note:

RewriteRule ^/folder/some_script$ ...

In a directory context, like a .htaccess file, this kind of rule will never match because of the ^/ since Apache truncates the path to remove the part of the path corresponding to this context including its last / (eg: if the path of the URL is /foo, the path tested by RewriteRule in a .htaccess file located at the DocumentRoot is only foo, not the string /foo).

  • Related