用PHP

2011-01-22 41 views
1
设置自定义网址

所以,我需要设置自定义的网址,以便用PHP

http://example.com/dynamic/

去:

http://example.com/dynamic.php

我知道这是一个非常简单的操作,但经过一番使用谷歌搜索,我找不到答案。处理这样的事情最简单的方法是什么?

+0

您需要在.htaccess中写入规则 – 2011-01-22 13:31:40

+0

另请参阅http://stackoverflow.com/questions/3631153/how-come-some-site-urls-do-not-include-a-file-extension/3631241#3631241 – Gumbo 2011-01-22 13:44:16

回答

1

有实现的几种方法这一点: -

  1. URL重写(附加在/动态/目录的.htaccess对服务器文件)
RewriteEngine on 
RewriteRule ^$ dynamic.php 
  1. 创建一个虚拟的index.htm /的.html/.php文件在根文件夹即。 /动态/
    如果HTML,请使用以下的javascript: -
<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: http://example.com/dynamic.php ");

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. 

希望这有助于。

6

你必须使用url重写来做到这一点。 取决于您正在使用的Web服务器。例如, ,如果您的Web服务器是Apache,则可以使用.htaccess。 Apache URL Rewriting.

4

为正则表达式将是这样的:

RewriteEngine on 
RewriteRule ([^/]+)/?$ $1.php [L] 

运行左边的正则表达式将返回“动态/”或“动态”如果不包括正斜杠。然后它将用dynamic.php替换第一个括号内的块(“dynamic /”)中捕获的内容。

你可以试试你的URL重写这里:

http://martinmelin.se/rewrite-rule-tester/

在这里,你一般的正则表达式:

http://regexpal.com/

+0

也许更好的正则表达式是([^ /] +)/ *?$,因为它将在最后解释多个正斜杠,但我不知道服务器是否将它们保留在那里:P – 2011-01-22 14:47:23