No announcement yet.

Apache's mod_rewrite and relative paths. A simple question?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Apache's mod_rewrite and relative paths. A simple question?

    I've managed to stump myself on something that I believe is rather simple, so I'm calling upon the collective brain of the vB community to see if what I am attempting to do is feasible.

    Let's say our vb install is located at I've made a very simple mod_rewrite rule as follows:

    RewriteRule ^/foo/$ /bar.php

    bar.php is a php script that outputs some text. It loads our vBulletin header and footer templates and functions like any other script in a vBulletin install.

    When you visit, since we didn't use a redirect ([R]), the URL in your browser stays as instead of

    I'm attempting to use mod_rewrite to make "pretty" (read: SEO friendly) urls for a webapp I'm writing. It is a review system of sorts, and rewrites will be in place for the following: -> /review.php -> /review.php?cid=X -> /review.php?pid=X

    The problem is, when you visit, all of the vB links in our navbars, to things like the usercp, search, etc, are all relative, and end up linking to "/foo/member.php" instead of "/member.php".

    Is there some type of trickery I can do, short of editing 140 some odd templates to make all of our vB links contain leading slashes (and subsequently having to edit them all later on should we move our forum to a subdirectory rather than /)?

    I have no problem creating the rules so I can create /foo/bar/baz/ rewrites to /foo.php?x=bar&y=baz, or anything of that nature, but can't seem to figure out how to either define something so that vBulletin doesn't think it is in a "virtual" subdirectory when it really isn't, or to make mod_rewrite rewrite an ENV variable or something of that nature.
    Kyle Christensen - one of the biggest and busiest vbulletin forums on the net!

  • #2
    Dag, all those views and no ideas?
    Kyle Christensen - one of the biggest and busiest vbulletin forums on the net!


    • #3
      I would reply if I was familiar with mod_rewrite.

      mod_rewrite is scary with all of its &#^^^*(#&)*^& stuff.

      There has to be one person around here who knows, though.


      • #4
        There is a html BASE attribute that may be of use.


        • #5
          There is an extension called mod_proxy_html from niq that might do what you wish without too much editing.

          Scott MacVicar

          My Blog | Twitter


          • #6
            You can still change all the URLs to be absolute at your app level - not necessarily by updating the templates, you can do it at the print_output() step by applying a sufficient PREG-fu.

            We're using the BASE HREF approach ourselves, but FYI apparently some browsers have trouble applying this tag to all URLs on a page, generating some funny looking 404s. The percentage of these broken user agents is low though, so this was the approach we've chosen.


            • #7
              Originally posted by merk View Post
              There is a html BASE attribute that may be of use.

              Yanno, I saw this when I was googling for solutions a while ago but it didn't click. It seems to be doing the job! I will report back once I have things worked out, but in my testing, just putting a base tag in the headinclude for my review scripts seems to be working.
              Kyle Christensen
     - one of the biggest and busiest vbulletin forums on the net!


              • #8
                Not sure how you are getting on, in our aplication we are using apaches mod_rewrite to create friendly URL's but I think unlike you all the articles are being rewritten to a .html extension ie

                RewriteRule ^/(.*)/a([0-9]+)/(.*)\.html$ /art.php?id=$2 [QSA,L]

                in our case the only important but is the element after the a which defines the article number the rest is cosmetic created by our aplication to make the whole thing look nice for example:


                The underlying application adds the digitaltv and "name of article" elements to the URL.

                Never seen the problem you are having with relative URLs.


                • #9
                  Well, I ended up fixing it with the BASE attribute, but I also opened up another can of worms.

                  I'll post my really messy "fix" here in case anyone else ever follows in my footsteps.

                  So, using mod_rewrite I am able to rewrite urls to appear prettier in the address bar. Using the base attribute I can make the vBulletin work, but then I open another can of worms on pages where I am using the vBulletin pagination functions, which always want to append &page=X onto your urls.

                  Furthermore, the javascript quick style switcher in the footer also wants to append styleid=X onto your urls to swap styles.

                  What I ended up doing to make everything work (don't cringe, it's a mess, I know) were rewrite rules like:

                  RewriteCond %{QUERY_STRING} ^styleid=([0-9]+)$
                  RewriteRule ^/reviews/products/([0-9].*)(|/.*)(|&page=[0-9]+)$  /review.php?pid=$1&styleid=%1$3 [L]
                  RewriteRule ^/reviews/products/([0-9].*)(|/.*)(|&page=[0-9]+)$  /review.php?pid=$1$3 [L]
                  2 rewrite rules per url, the first of which appends a styleid if it exists, and also the $3 portion of the url which is the &page=X if you are attempting to paginate.

                  I'm sure there is probably SOMETHING I could do to simplify them, but I fiddled a LOT and this was all I could come up with that worked
                  Last edited by KrON; Mon 30th Apr '07, 1:17pm.
                  Kyle Christensen
         - one of the biggest and busiest vbulletin forums on the net!


                  widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.