homepage Welcome to WebmasterWorld Guest from 54.205.207.53
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

    
Should this be done with .htaccess file or php script ?
jeddi



 
Msg#: 4621311 posted 3:16 pm on Nov 5, 2013 (gmt 0)

Hi,

I have a situation where I want to direct this
url : mypage.example.com
to: example.com/mypage/

BUT if the directory does not exist, then I want to direct it
to: example.com/info.php?pg=mypage

I don't think I can put if statements in the .htaccess file

but how would I do this in php script ?

Any suggestions ?

Thanks.


.

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4621311 posted 10:29 pm on Nov 5, 2013 (gmt 0)

I don't think I can put if statements in the .htaccess file

You can in Apache 2.4. But this isn't a full-blown "if"; it's the !-d condition in mod_rewrite. Not that this is any help, since the -d and -f lookups are decidedly a last resort. (In htaccess, it probably means the server has to look up the same thing twice. In config it may remember "Oh, right, I've already found this".)

Just how many filename-and-directory pairs are involved? If I were doing it in htaccess I'd constrain it to specific names, for example

RewriteRule ^(name|othername|foobar)\.html http://www.example.com/$1/ [R=301,L]

BUT if the directory does not exist, then I want to direct it
to: example.com/info.php?pg=mypage

Can't this be done equally well with a custom 404 page? Make the page in php and let it personalize the results depending on the page requests. Anything that wasn't covered in a specific filename-to-directory redirect will end up in 404 by default. If you've removed some pages, that's a 410, which can get its own page.

In any case you don't want to redirect (visibly) from
example.com/pagename.html
to
example.com/info.php?pg=pagename
because that's moving backward.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4621311 posted 8:07 am on Nov 6, 2013 (gmt 0)

the -d and -f lookups are decidedly a last resort. (In htaccess, it probably means the server has to look up the same thing twice. In config it may remember "Oh, right, I've already found this".)

this is very much filesystem-dependent - for example, whether there's caching and how well it's tuned for the application.

config vs .htaccess is simply a matter of when the directives are interpreted.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4621311 posted 8:17 am on Nov 6, 2013 (gmt 0)

..."Oh, right, I've already found this"

when the directives are interpreted

to finish that thought, the config file directives are interpreted upon server startup but they can't know what "this" might be be until the directives are executed for each request.
if the filesystem does a file/directory existence check and it's already been cached, there's minimal cost.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4621311 posted 8:45 am on Nov 6, 2013 (gmt 0)

I have a situation where I want to direct this
url : mypage.example.com
to: example.com/mypage/

BUT if the directory does not exist, then I want to direct it
to: example.com/info.php?pg=mypage

you have to be precise with your terminology here.

let me see if i can restate your original problem in a slightly different way and see if that's what you really want:

- if someone requests http://mypage.example.com/ and the directory /mypage/ exists within the document root directory, you want the visitor redirected to http://example.com/mypage/

in technical terms, this means the response includes a 301 status code and a Location: header referring to http://example.com/mypage/ so the browser subsequently requests the canonical url.
the browser's address bar ultimately shows http://example.com/mypage/


- if someone requests http://mypage.example.com/ and the directory /mypage/ doesn't exist within the document root directory, you want the request internally rewritten to /info.php?pg=mypage

the info.php script examines the query string and returns the appropriate response. (usually 200, 301 or 404/410)
the browser's address bar still shows http://mypage.example.com/ unless the response was a 301.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4621311 posted 12:01 pm on Nov 6, 2013 (gmt 0)

if someone requests http://example.com/mypage.html * and the directory /mypage/ doesn't exist within the document root directory, you want the request internally rewritten to /info.php?pg=mypage

If this interpretation is correct, then it almost certainly does make more sense to simply rewrite everyone this way. Then the php page can do the lookup and, if appropriate, issue a redirect. But a lot depends on how many files are involved.


* Edit here because I'm not 100% certain that what your fingers typed is what your brain meant.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4621311 posted 12:39 am on Nov 7, 2013 (gmt 0)

in my last post there i was addressing the OP's problem statement (as quoted) which didn't mention a file extension.

If this interpretation is correct, then it almost certainly does make more sense to simply rewrite everyone this way. Then the php page can do the lookup and, if appropriate, issue a redirect.

in any case it makes more sense to do this in the apache process instead of spawning a new process (with a rewrite) to make the same directory existence check.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4621311 posted 1:15 am on Nov 7, 2013 (gmt 0)

Cripes. My eyes saw
mypage.example.com
but the brain rendered it as
www.example.com/mypage

So the word "page" overrode the subdomain location. Oops.

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