Why does my .htaccess RewriteRule add my host server's subdirectory to rewrite URI?

Ok. Here is the deal. These rewrite things are wonky to write. They seem to have some variability depending on the server. So, rather than weed through a ton of posts that do not appear to be the same, I’m just going to ask for help. Hopefully the question will be useful to others…

Here is the .htaccess file,


    RewriteEngine on
    RewriteBase /apache_rewrite/a
    RewriteCond %{REQUEST_URI} ^(.*)$
    RewriteRule ^/([a-z/.]+)$ /b/$1 [R=301,L]

I am working on learning how the rewrite mod works and I created a sub-directory named apache_rewrite to test my rules.
The account’s root is

/home/my_account_name/

and my live folder is,

/home/my_account_name/public_html/

This is a shared host, with my own domain name, php
is run as cgi, and … rewrite works and I know it because my drupal site is working. What happens

This URI,

http://www.my_domain.com/apache_rewrite/a/test.html

Rewrites to this URI,

http://www.my_domain.com/home/my_account_name/public_html/apache_rewrite/^a/b/test.html

adding the directory info. Ignoring that the rewrite rule isn’t working (^a/b), why is the path being written to the URI?

simon,

That’s a problem with the Apache installation which may have been induced by the internal absolute redirection. I get that on my test server once in a while and merely restart Apache. However, on a shared server, it’s not likely that you can get the host to reset so work on that leading / in the redirection.

The second problem is with the RewriteBase as it’s designed to UNDO a mod_alias redirection and WILL cause problems if used indiscriminately. This is the case here, too.

The third problem (mine), is that I have no clue what you’re trying to do with

RewriteEngine on
RewriteBase /apache_rewrite/a
RewriteCond %{REQUEST_URI} ^(.*)$
# this is ALWAYS true (but does nothing for you)
RewriteRule ^/([a-z/.]+)$ /b/$1 [R=301,L]
# that should loop adding b/ with each loop execution

Regards,

DK

This appears to have been the case here. I’ve let this project sit for a few days. Apache must have reset, because its working now as written.

so work on that leading / in the redirection.

I don’t understand what you are suggesting–a way to circumvent the need to restart Apache?

The second problem is with the RewriteBase as it’s designed to UNDO a mod_alias redirection…

I don’t understand this. Are you saying RewriteRule is by default mod_alias?

The third problem (mine), is that I have no clue what you’re trying to do …

I’m just testing what the RewriteBase can do. Taking a page from you own excellent Apache mod_rewrite tutorials, I created several directories with inter-linked files to first test mod_rewrite was working, and then test RewriteBase. Overall a success really.

Would you care to offer any advice to limiting the application of tests only to a designated testing directory? You commented to the line: “RewriteCond %{REQUEST_URI} ^(.*)$” this is always true. Thus, this does noting to limit the conditional rule to the testing directory. Yet I assumed having the dot-htaccess file located in the testing directory would limit the rules to only the test-directory and its sub-directories.

is there anything else I can do to set up an environment to test Apache’s mod_rewrite rules?

Regards,

DK