.htaccess
如果您在 Apache 网络服务器上托管,请在与您的内容相同的文件夹中创建以下文件:
<IfModule mod_rewrite.c>
# Enable rewrite engine
RewriteEngine on
# If the condition "file does not exist"…
RewriteCond %{REQUEST_FILENAME} !-f
# or the condition "directory does not exist"…
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite the request as if it came from index.php
RewriteRule ^ index.php [L]
</IfModule>
对于 IIS 服务器,请尝试以下web.config
文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- Rewrite URLs of the form 'x' to the form 'index.php?q=x'. -->
<rule name="Short URLs" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?q={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/index.php" responseMode="ExecuteURL" />
</httpErrors>
<defaultDocument>
<!-- Set the default document -->
<files>
<remove value="index.php" />
<add value="index.php" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
两种配置都松散地基于Drupal 7配置文件。他们将向您的网络服务器发出与实际文件或目录(例如 /about-us)不匹配的所有请求,实际上返回输出(如果您正在提供静态 HTML 文件,则index.php
可以将其放在那里)。index.html
这一切都发生在服务器端……最终用户在输入 URL 时仍会看到该 URL ( http://yourdomain.com/about-us )。您可以使用 Javascript (as ) 捕获该 URL,location.href
并执行您需要执行的任何显示更改。