homepage Welcome to WebmasterWorld Guest from 54.166.228.100
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
using htaccess, to silently show another directory's contents
londrum




msg:4647827
 3:02 pm on Feb 22, 2014 (gmt 0)

i'm a bit of a newbie when it comes to using htaccess (and i'm also an idiot, which doesnt help), and this has got me stumped. hoping that someone can put me on the right path.

i have an empty directory
let's call it /blah/

and i want it to show the contents from a file in another directory
let's call it /example/file.php

and i need to do it silently, so the URL still shows as /blah/ in the browser

and to make it even more confusing, sometimes there will be query strings involved as well

so if a user visits
/blah/?u=10
i need it to silently show the contents from
/example/file.php?u=10

sometimes there are multiple query strings too, so if he visits
/blah/?u=10&b=99
i need it to silently show the contents from
/example/file.php?u=10&b=99

i have no idea if this is even possible with htaccess. maybe i'll just have to shift everything over manually. but if anyone has got any ideas i will much appreciate it

 

lucy24




msg:4648187
 10:08 pm on Feb 22, 2014 (gmt 0)

You can put the whole query-string question right out of your pretty little head, because query strings are reappended by default. That leaves you with the exceedingly simple matter of a silent rewrite, which looks like

RewriteRule ^visible-url-here-beginning-with-first-directory-name /target-url-here-with-leading-slash-for-root [L]

That's assuming you have other RewriteRules in place so you don't need the preliminary business with SymLinks and RewriteEngine. The rule goes at the end of your existing RewriteRules, since it's purely an internal rewrite.

Let's see your first try. You've been around since 2006 so we will cut you some slack, but come on, you can make one effort ;)

g1smd




msg:4648271
 10:38 am on Feb 23, 2014 (gmt 0)

You need a RewriteRule configured as an internal rewrite.

RewriteRule ^requested-filepath /internal-resource-that-serves-content [L]

Change the links on your pages to point to the new URLs.

Make sure your PHP returns HEADER 404 Not Found when a non-valid ID is requested.

As you are changing your URL format why not do away with the ? and = in the URL request?

londrum




msg:4648295
 12:08 pm on Feb 23, 2014 (gmt 0)

What do you do with the original folder though... where the info originally came from. Presumably you do a redirect to the new folder, otherwise you'll end up with duplicate content, But when the new folder tries to grab the stuff from the original filder, wont it just be redirected too.

Unless you are supposed to put this htacces in the root folder? At the moment i am putting it in the new folder. Maybe that is where i am going wrong.

londrum




msg:4648305
 1:58 pm on Feb 23, 2014 (gmt 0)

okay cool... i've got half of it working. i've put this in my htaccess
RewriteRule ^blah/ /example/file.php [L]

and it now shows the content from
/example/file.php
at
/blah/

and the query strings are working too.

but you can still visit
/example/file.php and see the same content, which is now duplicate content. is there any way to get that URL redirected to /blah/?
thanks for your help again

g1smd




msg:4648318
 2:50 pm on Feb 23, 2014 (gmt 0)

You need an additional RewriteRule configured as a redirect so that when the old URL is requested the server suggests that the browser makes a new request for the new URL. This new rule also needs a preceding RewriteCond looking at %THE_REQUEST to prevent an infinite loop.

Put all of your rules in the root htacccess file.

Put redirects before rewrites.

Redirects must include protocol and hostname in target URL.

Do not use Redirect or RedirectMatch directives for any of these rules. Use only RewriteRule (and RewriteCond).

It now shows the content from
/example/file.php
at
/blah/

Technically, when example.com/blah/ is requested the request is internally rewritten to instead fetch the content from folder and file at /example/file.php

lucy24




msg:4648373
 9:36 pm on Feb 23, 2014 (gmt 0)

What do you do with the original folder though... where the info originally came from. Presumably you do a redirect to the new folder, otherwise you'll end up with duplicate content, But when the new folder tries to grab the stuff from the original folder, wont it just be redirected too.

Oh, you're rewriting to a location that previously existed as a public, visible URL. That leads to:

This new rule also needs a preceding RewriteCond looking at %THE_REQUEST to prevent an infinite loop.

... where %{THE_REQUEST} means what the user originally asked for (and, incidentally, the only thing that will show up in logs when there's a rewrite). The formula is

RewriteCond %{THE_REQUEST} old-ugly-url
RewriteRule old-ugly-url http://www.example.com/new-pretty-url [R=301,L]


Since it's a redirect, this rule goes before the rewrite you just created.

londrum




msg:4648589
 3:25 pm on Feb 24, 2014 (gmt 0)

hmmm... i've tried a few different variations but i can't get this final bit to work
here is what i'm doing

RewriteCond %{THE_REQUEST} /example/file.php
RewriteRule /example/file.php http://localhost/blah/ [R=301,L]

i'm guessing it's something to do with the slashes or relative/absolute urls, but i can't figure out which bits to swap

g1smd




msg:4648590
 3:29 pm on Feb 24, 2014 (gmt 0)

RewriteCond pattern should begin ^[A-Z]{3,9}\ /exa...

RewriteRule pattern should begin ^ not /

Escape literal periods file\.php

lucy24




msg:4648625
 4:44 pm on Feb 24, 2014 (gmt 0)

RewriteCond %{THE_REQUEST} /example/file\.php
RewriteRule /example/file\.php http://example.com/blah/ [R=301,L]

Just to confuse you: the part in the Condition does start with a slash. It will look just like what you see in logs. But the body of the rule has no leading slash. Start with the directory name. For a top-level directory, use a ^ opening anchor. The ^ corresponds to the physical location of the htaccess file-- normally the root of one domain.

Oh, and
:: hasty edit here ::
Conditions and patterns should have \. escaped literal periods.

Further edit:
Oops. g1 said that already.

londrum




msg:4649062
 8:24 pm on Feb 25, 2014 (gmt 0)

RewriteCond %{THE_REQUEST} /example/file\.php
RewriteRule /example/file\.php http://example.com/blah/ [R=301,L]

me again. i cant get this final bit to work, try as i might
i have fiddled with slashes and backslashes and those little ^ things, but this last little bit is refusing to work. the code quoted doesnt appear to do anything

i have tried this to
RewriteCond %{THE_REQUEST} ^example/file\.php
RewriteRule /example/file\.php http://example.com/blah/ [R=301,L]

and
RewriteCond %{THE_REQUEST} ^example/file\.php
RewriteRule ^example/file\.php http://example.com/blah/ [R=301,L]

and all the variations of ^ and /

i am prepared to offer ten million pounds to whoever knows the mystical code. (i dont actually have 10 million pounds yet though, but if this code makes me rich then i will give you some)

g1smd




msg:4649072
 8:44 pm on Feb 25, 2014 (gmt 0)

RewriteCond RegEx pattern should begin ^[A-Z]{3,9}\ /exa...

RewriteRule RegEx pattern should begin with ^ not /

Escape literal periods file\.php

lucy24




msg:4649110
 10:43 pm on Feb 25, 2014 (gmt 0)

i cant get this final bit to work

That's because you overlooked the immediately preceding post :-P

londrum




msg:4649393
 3:53 pm on Feb 26, 2014 (gmt 0)

i've got it! it works perfect
thanks for your help. i'll stick the final thing here in case anyone else needs it

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*file\.php
RewriteRule ^(([^/]+/)*)file\.php /blah/ [R=301,L]
RewriteRule ^blah/ /example/file.php [L]

g1smd




msg:4649450
 6:25 pm on Feb 26, 2014 (gmt 0)

The redirect target should include protocol and hostname as per the earlier example.

Add a blank line after each Rule for clarity.

Add a comment before each ruleset describing what it does.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved