【问题标题】:PHP: How to catch text written in url after my domain namePHP:如何在我的域名之后捕获用 url 编写的文本
【发布时间】:2011-10-12 08:54:11
【问题描述】:

请参阅下面的示例链接/情景。

www.mydomain.com/khaled

“www.mydomain.com”上的默认页面是 index.php。没有名为“khaled”的文件夹或文件。输入上述 URL 后,我希望调用 index.php 并检查数据库中的名称“khaled”并显示相关配置文件。

问题:当我输入上述 URL 时显示错误 404。

.htaccess 是一个选项,但不知道在其中写什么。尝试了以下行,但它返回“内部服务器错误”。

RewriteRule ^([^/]+)$ index.php?i=$1&p=$2&%{QUERY_STRING} [L]

【问题讨论】:

    标签: php .htaccess url-rewriting


    【解决方案1】:

    $_SERVER['REQUEST_URI'] 将在 PHP 中保存此信息。您的 htaccess 重定向应该类似于:

    RewriteEngine On
    RewriteRule ^([^/\.]+)/?$ /index.php?i=$1 [L]
    

    然后在 index.php 中,$_GET['i'] 将保存 URL 中域之后的任何值

    你显然可以用同样的方式添加更多的参数,比如:

    RewriteEngine On
    RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?i=$1&j=$2 [L]
    

    然后在 PHP 中使用 $_GET['i']$_GET['j'] 作为变量

    【讨论】:

    • 感谢您的快速响应。每当我在 .htaccess 中输入任何内容时,我的浏览器都会出现错误“内部服务器错误”。注意:我当前的 .htaccess 是空的,我正在安装 WAMP 服务器的本地计算机上尝试。
    • 我已经尝试过您在我的网络服务器上工作的 .htaccess 建议:D 问题:在网络服务器上,应用程序的位置是“www.mydomain.com/app-folder/khaled”。您的代码最终出现在“www.mydomain.com/index.php”上。我需要“www.mydoamin.com/app-folder/index.php”来处理这个问题。请提出建议。
    • 您能发布您的 htaccess 中的其他内容吗?
    • 什么都没有。只有您建议的线路。
    • 在 httpd.conf 中是否开启了 mod-rewrite?
    猜你喜欢
    • 2020-10-26
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 2020-10-11
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多