7

我想让一个 PHP 文件捕获并管理用户访问时会发生什么:

http://profiles.mywebsite.com/ sometext

sometext是不同的。

例如,它可以是someuser也可以是john等。然后我想要一个 PHP 文件来处理来自该结构的请求。

我的主要目标是让某个 PHP 文件将我的站点用户重定向到他们相应的配置文件,但他们的配置文件与该 URL 结构不同。我的目标是为我的用户提供一种易于记忆的个人资料 URL。

感谢那些愿意回答的人!

4

5 回答 5

18

在 Apache 配置文件 [VirtualHost 或 Directory 指令] 或 .htaccess 文件中放入以下行:

Options -MultiViews

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L,NC,QSA]
</IfModule>

它会静默地将所有与有效文件名或目录不对应的传入请求(RewriteCond上面代码中的 's 确保这一点)重定向到 index.php 文件。此外,如您所见,MultiViews还需要禁用选项才能重定向工作 - 它通常与RewriteCond我放在那里的这两个冲突。

在 index.php 中,您可以通过变量访问REQUEST_URI数据。$_SERVER['REQUEST_URI']您不应该通过 传递任何 URI GET,因为它可能会以不希望的方式污染您的 Query-String 数据,因为[QSA]我们的 RewriteRule 中的参数是活动的。

于 2011-03-07T10:07:20.457 回答
5

你应该使用重写规则..

在 apache (.htaccess) 中,是这样的:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>

然后在您的 index.php 中,您可以在 php 代码中读取 $_GET['url'] 。

于 2011-03-07T10:03:23.887 回答
0

您可以使用 .htaccess 文件 ( http://httpd.apache.org/docs/1.3/howto/htaccess.html ) 将您的 url 重写为 profiles.websites.com/index.php?page=sometext 之类的内容。然后你可以用 index.php 中的 sometext 做你想做的事。

于 2011-03-07T10:01:51.487 回答
0

一个明显的方法是通过 404 errorDocument - 保存所有与 mod_rewrite 相关的麻烦。

于 2011-03-07T10:20:04.923 回答
-6

如果你还没有听说过 MVC,是时候听听它了,从 CodeIgniter 开始,它最简单而且速度很快,使用默认控制器,你可以拥有类似的 URL
domain.com/usernam/profile
domain.com/usernam/profile/edit
domain.com/usernam/inbox
domain.com/usernam/inbox/read/messageid或者明智地使用 .htaccess

于 2011-03-07T10:04:19.097 回答