【发布时间】:2011-01-22 13:27:13
【问题描述】:
所以,我需要设置自定义 URL,以便
前往:
http://example.com/dynamic.php
我知道这是一个非常简单的操作,但经过一番谷歌搜索后,我找不到答案。处理这种情况最简单的方法是什么?
【问题讨论】:
-
你需要在.htaccess中写规则
所以,我需要设置自定义 URL,以便
前往:
http://example.com/dynamic.php
我知道这是一个非常简单的操作,但经过一番谷歌搜索后,我找不到答案。处理这种情况最简单的方法是什么?
【问题讨论】:
您必须使用 url 重写来做到这一点。 这取决于您使用的 Web 服务器。 例如,如果您的网络服务器是 Apache,您可以使用 .htaccess。 Apache URL Rewriting.
【讨论】:
正则表达式是这样的:
RewriteEngine on
RewriteRule ([^/]+)/?$ $1.php [L]
如果不包含正斜杠,则在左侧运行正则表达式将返回“dynamic/”或“dynamic”。然后它将用dynamic.php替换第一个带括号的块(“dynamic/”)中捕获的内容。
您可以在此处尝试您的 URL 重写:
这里是你的通用正则表达式:
【讨论】:
有几种方法可以实现这一点:-
RewriteEngine on RewriteRule ^$ dynamic.php
<script type="text/javascript"> window.location = "http://example.com/dynamic.php" </script> <script type="text/javascript"> location.href='http://example.com/dynamic.php'; </script> <script type="text/javascript"> location.replace('http://example.com/dynamic.php'); </script>
However, if using PHP instead (recommended), include the following:-
header("Location: @987654321@");
Remember to use ob_start(), ob_end_flush() to fix the output buffer, if you get the error that the headers are already sent and you can't modify the header information.
希望这会有所帮助。
【讨论】: