【问题标题】:htaccess rewrite give error 500 in localhosthtaccess 重写在本地主机中给出错误 500
【发布时间】:2014-04-02 17:30:33
【问题描述】:

我一直在网上搜索,但似乎没有任何效果。我正在使用 Apache 和 Xampp 在本地工作。我创建了一个 .htaccess 文件并重写了所有以 .php 和 .html 结尾的 url 以删除扩展名,并创建了一个自定义 404 页面。一切正常。但是,现在我正在尝试重写一个看起来像

的动态 url

/TotinCoblan/update/Wine?id=2

/TotinCoblan/update/Wine/2

我不断收到错误 500。这是我的 .htaccess 文件。据我了解,

RewriteCond %{REQUEST_FILENAME} !-f

应该避免无限循环。

任何帮助将不胜感激。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L]

ErrorDocument 404 TotinCoblan/errors/404

【问题讨论】:

    标签: php apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    试试这个代码:

    ErrorDocument 404 /TotinCoblan/errors/404
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteRule ^update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L,QSA]
    
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html
    

    【讨论】:

    • 谢谢!它工作得很好。但是我意识到我的 css 文件没有正确加载,所以我必须在所有文件中使用绝对路径。
    • 是的,对 css/js/images 使用绝对路径总是更好。如果有效,请考虑接受答案。
    【解决方案2】:

    我终于使用了这段代码,我不得不使用 RewriteCond %{REQUEST_FILENAME} !-f 因为页面只有在重新加载两次后才会加载。使用该指令首次加载。

    RewriteCond %{REQUEST_FILENAME} !-f
    ErrorDocument 404 /TotinCoblan/errors/404
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ - [L]
    
    RewriteRule ^update/Wine/([0-9]+)/?$ update/Wine.php?id=$1 [L,QSA]
    
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html
    

    【讨论】:

      【解决方案3】:

      您应该将 -f 检查更改为使用 %{DOCUMENT_ROOT}${REQUEST_URI}%{REQUEST_FILENAME} 的问题是 mod_rewrite 将部分尝试将 URI 映射到文件,并且它会考虑 PATH INFO,这意味着如果请求类似于:

      /something/foo/anything/
      

      你碰巧有一个现有的文件/脚本在:

      /something/foo.php
      

      那么RewriteCond %{REQUEST_FILENAME}\.php -f 检查将是真的,它假定你的意思是:

      /something/foo.php/anything/
      

      并且,就像您的代码所做的那样,.php 被附加到末尾:

      /something/foo/anything/.php
      

      这会导致无限循环。尝试类似:

      RewriteEngine On
      
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
      RewriteRule ^(.*)$ $1.php
      
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.html -f
      RewriteRule ^(.*)$ $1.html
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^/update/Wine/([0-9]+)/?$ /update/Wine?id=$1 [L]
      
      ErrorDocument 404 TotinCoblan/errors/404
      

      【讨论】:

      • 谢谢,我尝试了您的代码,但没有 .php 和 .html 文件时出现 404 错误......下面的代码也适用于这些情况。谢谢
      • @amunte 抱歉,$ 应该是 %
      猜你喜欢
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 2014-09-25
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多