treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] HTAccess - Fake directory to actual subdirectory.

  • Hey guys,

    I'm trying to rewrite a URL but not having much luck, it is a Wordpress site, so not sure if that makes a difference.

    I'm trying to make it so someone could go to http://domain.com/demos/FILENAME.php

    The actual file path would be http://domain.com/wp-content/themes/themename/demos/FILENAME.php

    I've tried a few so far like;

      RewriteRule ^demos/(.*) /wp-content/themes/themename/$1
    

    So far having no luck though - any tips?

  • Your code loses the demos part in the regex. You need to include it.

    RewriteRule ^demos/(.*) /wp-content/themes/themename/$1
    

    $1 will read FILENAME.PHP, so the final URL becomes /wp-content/themes/themename/FILENAME.PHP.

    Try:

    RewriteRule ^demos/(.*) /wp-content/themes/themename/demos/$1
    
  • Hi mate,

    Ah that was a typo when I created the thread - I was already doing your changed one, no luck unfortunately.

    Here's what I have inside the HTAccess file;

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    RewriteCond %{HTTP_HOST} !^codeboxers.com$ [NC]
    RewriteRule ^(.*)$ http://codeboxers.com/$1 [L,R=301]
    RewriteRule ^demos/(.*) /wp-content/themes/codeboxers/demos/$1
    

  • RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    

    This block combined tells the server that if the request doesn't point to an existing file or directory, then re-route it to index.php. Your later rewrite rule can't be reached. Put it earlier.

  • @benwalker Hmm, sadly that doesn't work either.

  • Could you post again what you are using. I moved the line beneath the RewriteBase and it worked fine. I would suggest finishing the line with [L] if you aren't looking to further process the URL.

  • Ah! That did it, moving it below the Rewrite base. Final completed version below;

      # BEGIN WordPress
      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule ^demos/(.*) /wp-content/themes/codeboxers/demos/$1 [L]
      RewriteRule ^index\.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]
      RewriteCond %{HTTP_HOST} !^codeboxers.com$ [NC]
      RewriteRule ^(.*)$ http://codeboxers.com/$1 [L,R=301]
    
      </IfModule>
    
      # END WordPress
    
  • Sorry -- I should have been more specific when I said "put it earlier" :-)

    I'm glad you've got it working.

  • No worries - thanks for your help mate, it was driving me nuts.