在 PHP 中创建一个 Catch-All 处理程序?
我想要一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Apache 配置文件 [VirtualHost 或 Directory 指令] 中,或者在 .htaccess 文件中放置以下行:
它将默默地重定向所有与有效文件名或目录不对应的传入请求(
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:
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 twoRewriteCond
'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 viaGET
, as it may pollute your Query-String data in an undesired way, since[QSA]
parameter in our RewriteRule is active.你应该使用重写规则。
在 apache (.htaccess) 中,类似这样:
然后在你的 index.php 中你可以在你的 php 代码中读取 $_GET['url'] 。
You should use a rewrite rule..
In apache (.htaccess), something like this:
Then in your index.php you can read $_GET['url'] in your php code.
您可以使用 .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.
一个明显的方法是通过 404 errorDocument - 保存所有与 mod_rewrite 有关的混乱。
An obvious way to do this would be via the 404 errorDocument - saves all that messing about with mod_rewrite.
如果您还没有听说过 MVC,那么您该听听它了,从 CodeIgniter 开始,它最简单且速度相当快,使用默认控制器,您可以拥有类似
domain.com/usernam/profile
domain.com/usernam/profile/edit
domain.com/usernam/inbox
domain.com/usernam/inbox/read/messageid
或者明智地使用 .htaccessIf 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