dtgj8529 2017-10-28 21: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-29 01: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条)

报告相同问题?

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline