0

以下 .htacess 文件在每个 url 上引发服务器错误,并且还破坏了图像/css 引用。

此 modrewrite 取自Neil Crosby 的回答 mod_rewrite 以删除 .php 但仍提供 .php 文件?

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

我需要发生的是:

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

  1. 如果用户试图加载/something.php,他们应该被外部重定向到/something/。
  2. 如果用户试图加载 /something 那么他们应该被内部重定向到 /something.php。
  3. 如果用户将任何查询字符串参数传递给 URL,那么这些参数应通过重定向保留。
  4. 如果用户尝试加载文件系统中确实存在的不同文件(样式表、图像等),则应按原样加载。

这与 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 的基本了解非常少,因此对于分解每个命令及其含义的任何帮助也将不胜感激。

4

0 回答 0