doudang4568
2010-02-02 14:01
浏览 26
已采纳

如何阻止不存在的URL解析为文件?

I don't know if this is an Apache or PHP thing but if you go to either of:

mysite.com/about
mysite.com/about/

it will show the mysite.com/about.php file instead of a 404 error, because the directory does not exist.

I am sure this is some setting, maybe an htaccess thing, any ideas?

图片转代码服务由CSDN问答提供 功能建议

我不知道这是Apache还是PHP的东西,但如果你去了以下任何一个:

  mysite.com/about
mys​​ite.com/about/
   
 
 

它将显示 mysite.com /about.php 文件而不是404错误,因为该目录不存在。

我确定这是一些设置,也许是一个htaccess的东西,任何想法?< / p>

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglin317704291 2010-02-02 14:11
    已采纳

    As others have answered it might be a mod_rewrite rule; there is also a directive called MultiViews that is explicitly for enabling the functionality you describe.

    http://httpd.apache.org/docs/2.2/content-negotiation.html#multiviews

    The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.

    Look for

    Options MultiViews
    

    in your httpd.conf or virtualhost-specific config file.

    已采纳该答案
    打赏 评论
  • drpogkqqi536984960 2010-02-02 14:04

    That's not default behavior, for sure, so it's hard to say. If that were happening on the Apache side, look for a sequence of rewrite matching instructions; they'll likely begin with RewriteCond and ending with RewriteRule.

    If you don't see anything like that in your VirtualHost config or site .htaccess, it's being managed by whatever PHP framework you're using, and you'll need to configure things there instead.

    打赏 评论
  • doujing4555 2010-02-02 14:24

    with this .htaccess you can parse, with $_SERVER[REQUEST_URI] in the PHP side, the complete URL and then perform your desired action, this is a general way, you should consider error controlling and 404 redirection:

    ErrorDocument 404 /404.php
    <IfModule mod_rewrite.c>
    RewriteEngine On
    # RewriteBase 
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    </IfModule>
    

    and int the PHP side, that should be this index.php

    $URLarray = explode('/', $_SERVER['REQUEST_URI']);
    
    打赏 评论

相关推荐 更多相似问题