【问题标题】:htaccess rewritecond not workinghtaccess rewritecond 不工作
【发布时间】:2012-11-02 10:17:08
【问题描述】:

希望你能帮上忙。我正在尝试编写一个 htaccess 文件,请执行以下操作。

1) 重定向到 www。地址

2) 从 url 中删除 .php

3) 如果文件不存在,则使用 filechecker.php?page=filename


1 我可以用

RewriteCond %{HTTP_HOST} ^example.com$

重写规则 (.*) http://www.example.com/$1 [R=301,L]


2) 我可以用

RewriteCond %{SCRIPT_FILENAME}.php -f

重写规则 [^/]$ %{REQUEST_URI}.php [QSA,L]


3) 我想

RewriteCond %{REQUEST_FILENAME}.php !-f

RewriteRule ^([^/]*)$ filechecker.php?page=$1 [QSA,L]

会起作用,但由于某种原因,它忽略了页面确实存在的事实。


希望你能帮忙 标记

【问题讨论】:

    标签: php apache .htaccess


    【解决方案1】:

    您对 2 的解决方案将循环,但很容易修复,请在您的文件中为第 2 部分和第 3 部分添加以下内容:

    #If the Browser request contains a .php, instruct the browser to remove it.
    RewriteCond %{THE_REQUEST}      \.php      [NC]
    RewriteRule ^/?(.*)\.php$       http://%{HTTP_HOST}/$1         [R=301,NC,L]
    
    #If a request is received for a non file-system object, that doesn't have a .php suffix, then store the full path, filename, and URI in a variables with that extention.
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-s
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !^\.php$  [NC]
    RewriteRule ([^/]+)$      -  [E=testScript:%{SCRIPT_FILENAME}.php,E=testFile:$1.php,E=testURI:%{REQUEST_URI}.php]
    
    #See if the file exists with a .php extention, if it does internally rewrite
    RewriteCond %{ENV:testScript} -s  [OR]
    RewriteCond %{ENV:testScript} -f
    RewriteRule .*              %{ENV:testURI} [L]
    
    #Else if a ENV:testDile is set, then pass the name to the php script
    RewriteCond %{ENV:testFile} !^$
    RewriteRule .*               /filechecker.php?page=%{ENV:testFile} [L]
    

    仅供参考:Apache mod_rewrite 文档值得一读,如果您不清楚上述规则的作用,或者如果您想要更抽象的内容,请考虑以下 post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-06
      • 2023-01-25
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 2015-01-27
      • 2014-06-24
      • 1970-01-01
      相关资源
      最近更新 更多