URL路径重定向到一个php文件

URL path redirects to a single php file

本文关键字:一个 php 文件 路径 重定向 URL      更新时间:2023-09-26

想知道是否有可能获得不同的URL路径重定向到单个php文件,我可以从URL中提取名称。

。如果我有www.example.com/username或www.example.com/username2或www.example.com/username3

我希望它重定向到一个username.php文件,然后我可以从URL获取用户名,并从数据库加载信息。

如果没有,有没有人有任何想法,我如何才能做类似的事情。Tumblr做了类似的事情,你可以在url中输入用户名,它就会加载他们的博客。

是的,当然这是可能的,感谢URL重写!查看Apache mod重写。

对于您的信息,您想要的重写规则可能看起来像:

RewriteEngine On
RewriteRule ^/username(.*) /username.php?user=username$1 [QSA,L]

我很确定这将工作。只需在您的服务器中激活mod_rewrite

打开www.example.com/page/usernameweb服务器将重定向到www.example.com/index.php?username=username1这样你就可以给每个用户一个页面

实际上是像

这样的页面
www.example.com/index.php?username=username1
www.example.com/index.php?username=username2
www.example.com/index.php?username=username3

使用重写引擎,它们可以像这样访问

www.example.com/page/username
www.example.com/page/username2
www.example.com/page/username3

. htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^page/(.*) /index.php?username=$1 [QSA,L]

是的,这被称为FrontController模式,通常通过重写规则实现。Apache2的一个稍微不同的方法示例如下:

<VirtualHost *:80>
  ServerName quickstart.local
  DocumentRoot /path/to/quickstart/public
  SetEnv APPLICATION_ENV "development"
  <Directory /path/to/quickstart/public>
      DirectoryIndex index.php
      AllowOverride All
      Order allow,deny
      Allow from all
  </Directory>
</VirtualHost>

这个来自Zend框架手册,在那里你可以找到一个FrontController模式的实际例子。基本上,所有请求都被发送到一个文件(在本例中为index.php),并从那里处理,通常通过将请求路由到另一个控制器。