Why is the RewriteBase not working?

6,863

There are a couple things in play here. First, the Alias directive wants its right-hand-side to be an absolute, physical path on the server: you want

Alias /runs /mysite/xhprof/xhprof_html

<Directory /mysite/xhprof/xhprof_html>
    Order allow,deny
    Allow from all
    AllowOverride All
</Directory>

Secondly, the RewriteRule RewriteRule .* index.php matches not only http://.../runs, but also any URL beginning with http://.../runs/, even, for example, http://.../runs/css/.... There are a couple ways to get around that.

 

Option 1: you could have a RewriteRule only redirect the root of the runs to index.php:

    RewriteRule ^$ index.php
    RewriteRule ^/$ index.php

 

Option 2: you could have the your mod_rewrite configure special-case things that exist as files, and redirect everything else to index.php

    # Require the path the request translates to is an existing file
    RewriteCond %{REQUEST_FILENAME} -f
    # Don't rewrite it, but do stop mod_rewrite processing
    RewriteRule .* - [L]

    # Now, redirect anything into index.php
    RewriteRule .* index.php

 

Option 3: you can special-case certain URLs, and redirect everything else to index.php

    RewriteCond $1 !^css/
    RewriteCond $1 !^js/
    RewriteRule .* index.php

 

Option 4: if you want any URL mapping to a directory to show an index.php file (like index.html), there's a really simple way, which is probably what you want. You can put the following either in a .htaccess or inside the <Directory> block in directories.conf:

    DirectoryIndex index.php index.html

 

Footnote: the RewriteRules above basically throw away all of the URL for any request that ends up mapping to index.php. That includes query-strings, so /runs/?foo=bar is the same as /runs/. If that's not what you want, you need a rule like

    RewriteRule ^(.*)$ index.php/$1 [QSA]

which preserves both the path-info (the $1 part) and the query-string ("QSA" = "query-string append".)

Have I written way too much yet? :)

Share:
6,863

Related videos on Youtube

ThinkingMonkey
Author by

ThinkingMonkey

Free-Thinker, Individualist, INTP

Updated on September 18, 2022

Comments

  • ThinkingMonkey
    ThinkingMonkey over 1 year

    Here is what I am trying to do:

    • domain is thinkingmonkey.me
    • domain has 127.0.0.1 as IP addr
    • mod_alias is installed.

    I have a conf file called directories.conf. In which I have all the configuration pertaining to directories. directories.conf is included in httpd.conf

    My directories.conf has

    Alias /runs /xhprof/xhprof_html
    
    <Directory /mysite/xhprof/xhprof_html>
        Order allow,deny
        Allow from all
        AllowOverride All
    </Directory>
    

    In /mysite/xhprof/xhprof_html/.htaccess. I have the following:

    RewriteEngine on
    RewriteBase /runs
    RewriteRule  .*  index.php
    

    All I am trying to do is to direct any request under /mysite/xhprof/xhprof_html/ to index.php.

    When I request for thinkingmonkey.me/runs with no trailing slashes I get 404 not found.

    So, I infer that RewriteBase is not working.
    What am I doing wrong?

    • user260399
      user260399 over 12 years
      In the Alias line, is it a typo or intentional that "/mysite/" isn't included? The right-hand-side of an Alias directive should be the actual path on the filesystem. Also, can you post the last couple lines of your /var/log/apache2/access.log and .../error.log?
    • ThinkingMonkey
      ThinkingMonkey over 12 years
      @jon It was intentional. Thanks for mentioning it. Alias: Since it was not mentioned anywhere in the docs I thought a relative path could be given.
    • ThinkingMonkey
      ThinkingMonkey over 12 years
      @jon although, index.php is loading, the js, css which is present in the folder /mysite/happ/xhprof/xhprof_html/css does not. Looking into this now.
    • ThinkingMonkey
      ThinkingMonkey over 12 years
      @jon ya realised that sometime back, writing rules to redirect for js & css folders. Thanks. If possible please post you comment as an answer. Will accept it.
  • user260399
    user260399 over 12 years
    This question had good timing: I reworked a .htaccess that dealt with basically exactly this stuff really recently, so it's all really fresh in my mind.
  • ThinkingMonkey
    ThinkingMonkey over 12 years
    ha ha nice! :).