以下modrewrite生成服务器错误,我无法解决原因

时间:2013-04-22 21:36:55

标签: regex apache mod-rewrite

以下.htacess文件会在每个网址上引发服务器错误,并且还会破坏images / css引用。

此modrewrite来自Neil Crosby's回答 mod_rewrite to remove .php but still serve the .php file?

唯一的变化是我将域名更改为.co.nz域名。

我需要做的是:

对于此解决方案,我遵循以下规则:

  1. 如果用户尝试加载/something.php,则应将其外部重定向到/ something /.
  2. 如果用户尝试加载/某些内容,则应将其内部重定向到/something.php。
  3. 如果用户将任何查询字符串参数传递给URL,则应通过重定向保留这些参数。
  4. 如果用户尝试加载文件系统中真正存在的其他文件(样式表,图像等),则应按原样加载。
  5. 这与modrewrite应该是完全一样的,除非它抛出服务器错误。

    除此之外,我还想检查是否有引用的目录或子目录,所以我认为以下添加也会解决这个问题。

    RewriteCond %{REQUEST_FILENAME} !-d
    

    这是原始的modrewrite,这会导致服务器错误: - 主URL(例如没有尾随斜杠) - 存在的直接文件(例如/file-name.php) - 存在的真实目录(例如/ directory,该目录包含索引) - 位于不同目录中的css和图像,它们似乎正在破碎。

    RewriteEngine on
    RewriteBase /
    
    ## Always use www.
    RewriteCond %{HTTP_HOST} ^domain\.co\.nz$ [NC]
    RewriteRule ^(.*)$ http://www.domain.co\.nz/$1 [L,R=301]
    
    # Change urlpath.php to urlpath
    ## Only perform this rule if we're on the expected domain
    RewriteCond %{HTTP_HOST} ^www\.domain\.co\.nz$ [NC]
    
    ## Don't perform this rule if we've already been redirected internally
    RewriteCond %{QUERY_STRING} !internal=1 [NC]
    
    ## Redirect the user externally to the non PHP URL
    RewriteRule ^(.*)\.php$ $1 [L,R=301]
    
    # if the user requests /something we need to serve the php version if it exists
    
    ## Only perform this rule if we're on the expected domain
    RewriteCond %{HTTP_HOST} ^www\.domain\.co\.nz$ [NC]
    
    ## Perform this rule only if a file with this name does not exist
    RewriteCond %{REQUEST_FILENAME} !-f
    
    ## Perform this rule if the requested file doesn't end with '.php'
    RewriteCond %{REQUEST_FILENAME} !\.php$ [NC]
    
    ## Only perform this rule if we're not requesting the index page
    RewriteCond %{REQUEST_URI} !^/$
    
    ## Finally, rewrite the URL internally, passing through the user's query string
    ## using the [qsa] flag along with an 'internal=1' identifier so that our first
    ## RewriteRule knows we've already redirected once.
    RewriteRule ^(.*)$ $1.php?internal=1 [L, QSA]
    

    此外,我对modrewrite和regex的基本理解非常小,所以任何有关打破每个命令及其含义的帮助也将非常感激。

0 个答案:

没有答案