Grow your CSS skills. Land your dream job.

HTAccess – Fake directory to actual subdirectory.

  • # January 3, 2013 at 8:24 am

    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?

    # January 3, 2013 at 8:47 am

    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

    # January 3, 2013 at 9:11 am

    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
    # January 3, 2013 at 1:07 pm

    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.

    # January 3, 2013 at 2:45 pm

    @benwalker Hmm, sadly that doesn’t work either.

    # January 3, 2013 at 3:19 pm

    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.

    # January 3, 2013 at 3:24 pm

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

    # BEGIN WordPress

    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]

    # END WordPress

    # January 3, 2013 at 3:27 pm

    Sorry — I should have been more specific when I said “put it earlier” :-)

    I’m glad you’ve got it working.

    # January 3, 2013 at 3:35 pm

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

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".