在 PHP 中创建一个 Catch-All 处理程序?

发布于 2024-10-21 04:12:10 字数 364 浏览 13 评论 0原文

我想要一个 PHP 文件捕获并管理用户访问时会发生的情况:

http://profiles.mywebsite.com/sometext

sometext 是变化的。

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

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

感谢那些愿意回答的人!

I want to have a PHP file catch and manage what's going to happen when users visit:

http://profiles.mywebsite.com/sometext

sometext is varying.

E.g. It can be someuser it can be john, etc. then I want a PHP file to handle requests from that structure.

My main goal is to have that certain PHP file to redirect my site users to their corresponding profiles but their profiles are different from that URL structure. I'm aiming for giving my users a sort of easy-to-remember profile URLs.

Thanks to those who'd answer!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌生 2024-10-28 04:12:10

在 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 中的上面的代码确保这一点),到index.php文件。此外,如您所见,还需要禁用 MultiViews 选项才能使重定向正常工作 - 它通常与我放在那里的两个 RewriteCond 冲突。

在index.php中,您可以通过$_SERVER['REQUEST_URI']变量访问REQUEST_URI数据。您不应通过 GET 传递任何 URI,因为它可能会以不希望的方式污染您的查询字符串数据,因为我们的 RewriteRule 中的 [QSA] 参数处于活动状态。

Either in Apache configuration files [VirtualHost or Directory directives], or in .htaccess file put following line:

Options -MultiViews

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

It will silently redirect all incoming requests that do not correspond to valid filename or directory (RewriteCond's in the code above make sure of that), to index.php file. Additionally, as you see, MultiViews option also needs to be disabled for redirection to work - it generally conflicts with these two RewriteCond's I put there.

Inside index.php you can access the REQUEST_URI data via $_SERVER['REQUEST_URI'] variable. You shouldn't pass any URIs via GET, as it may pollute your Query-String data in an undesired way, since [QSA] parameter in our RewriteRule is active.

凉薄对峙 2024-10-28 04:12:10

你应该使用重写规则。

在 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'] 。

You should use a rewrite rule..

In apache (.htaccess), something like this:

<IfModule mod_rewrite.c>
    RewriteEngine On

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

Then in your index.php you can read $_GET['url'] in your php code.

榕城若虚 2024-10-28 04:12:10

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

You can use a .htaccess file (http://httpd.apache.org/docs/1.3/howto/htaccess.html) to rewrite your url to something like profiles.websites.com/index.php?page=sometext . Then you can do what you want with sometext in index.php.

薄荷→糖丶微凉 2024-10-28 04:12:10

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

An obvious way to do this would be via the 404 errorDocument - saves all that messing about with mod_rewrite.

谁与争疯 2024-10-28 04:12:10

如果您还没有听说过 MVC,那么您该听听它了,从 CodeIgniter 开始,它最简单且速度相当快,使用默认控制器,您可以拥有类似
domain.com/usernam/profile
domain.com/usernam/profile/edit
domain.com/usernam/inbox
domain.com/usernam/inbox/read/messageid 或者明智地使用 .htaccess

If you have not heard about MVC, its time you hear it, start with CodeIgniter, its simplest and is quite fast, use default controller and you can have URLs like
domain.com/usernam/profile
domain.com/usernam/profile/edit
domain.com/usernam/inbox
domain.com/usernam/inbox/read/messageid Or use .htaccess wisely

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文