dtgj8529 2017-10-28 13:21
浏览 17
已采纳

在添加索引之前,htaccess不会加载索引

I am trying to remove folder from the URL and I did that, but the problem is when i visit domain.com it shows error 500, but if i add domain.com/index.php it will work.

RewriteEngine On
RewriteBase /insta/member/

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+insta/member/pages/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^insta/member/pages)^(.*)$ /insta/member/pages/$1 [L,NC]
  • 写回答

2条回答 默认 最新

  • dongshi1914 2017-10-28 17:24
    关注

    In this set of rules, looks like the requests are considered to always be files and if the request file name is not a file, then it adds .php to it. It is not always the case though. In apache 2.2 mode_rewrite

    REQUEST_FILENAME : The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI

    Therefore, the REQUEST_FILENAME may be resolved to files, folders or directories (this may vary in other web services or other apache versions). In case of files the rules work otherwise it fails. For example //domain.com will be resolved to the document root. But //domain.com/index.php will be resolved to the index.php file in the document root. To avoid these kind of confusions, all three possibilities should be considered, generally speaking. Also the REQUEST_FILENAME may be a file by itself without .php like //domain.com/my_styles.css. In that case the rules fail too. Following set of rules should work

        RewriteEngine On
        RewriteBase /insta/member/
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME}.php -f
        RewriteCond %{REQUEST_URI} !/$
        RewriteRule (.*) $1\.php [L]
    
        RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+insta/member/pages/([^\s]+) [NC]
        RewriteRule ^ %1 [R=301,L]
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule (?!^insta/member/pages)^(.*)$ /insta/member/pages/$1 [L,NC]
    

    Edit: Explanation for first set of rules is that it is checking if the resolved filename part of the request is not a directory, link or file and "filename.php" is a file and it is not ending with forward slash then add .php to the request.

    Answering to the question in comment about pretty url:

    To achieve pretty url you need to know the application routing. For removing a GET parameter from query string like ?checking=value rule should be like following

    RewriteRule (.*)/checking/(.*)$ $1?checking=$2 [L]
    

    It will be like following to combine with the above rules

        RewriteEngine On
        RewriteBase /insta/member/
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteRule (.*)/checking/(.*)$ $1\.php?checking=$2 [QSA,L]
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteCond %{REQUEST_FILENAME}.php -f
        RewriteRule (.*) $1\.php [QSA,L]
    

    Then $_GET values for following examples would be

        http://localhost/insta/member/test
        array (size=0)
          empty
    
        http://localhost/insta/member/test?checking=value
        array (size=1)
          'checking' => string 'value' (length=5)       
    
        http://localhost/insta/member/test/checking/value
        array (size=1)
          'checking' => string 'value' (length=5)
    
        http://localhost/insta/member/test/checking/value?second=2
        array (size=2)
          'checking' => string 'value' (length=5)
          'second' => string '2' (length=1)
    

    Finally, here is the specific rule for check/ig/value to check?ig=value going to check.php?ig=value

        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteRule (.*)/(.*)/(.*)$ $1.\php?$2=$3 [QSA,L]
    

    Sample output for GET

        http://localhost/insta/member/check/ig/value
        array (size=1)
          'ig' => string 'value' (length=5)
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部