【问题标题】:htaccess rewriterule query to urlhtaccess rewriterule 查询到 url
【发布时间】:2014-07-30 08:49:49
【问题描述】:

我正在努力做到这一点:

http://www.testsite.com/bacon

就这样

http://www.testsite.com/index.php?food=bacon

因此用户输入/点击链接为http://www.testsite.com/bacon,但服务器将其解释为http://www.testsite.com/index.php?food=bacon

对任何解决方案的一些解释也很有价值。

【问题讨论】:

    标签: .htaccess get query-string


    【解决方案1】:

    您可以在 .htaccess 文件中使用此代码为您的任务创建一个虚 URL

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME}.php -f  
    RewriteRule ^([^\.]+)$ $1.php [NC]
    RewriteCond %{REQUEST_FILENAME} >""
    RewriteRule ^([^\.]+)$ index.php?food=$1
    

    说明

    1. 我们的第一行打开了重写引擎。
    2. 以上行检查请求是否为文件
    3. 此行转而添加 .php 扩展名。因此,例如,如果您的 URL 是 /friends,则将调用 friends.php 文件。(因此,根据您的用例,这部分是可选的)
    4. 最后两行提取不是文件名的 url,例如 your-url.com/philipbrown 并将其路由到 profile.php?user=philipbrown。

    访问this link了解更多信息

    【讨论】:

    • 不适合我。该网站说testsite.com/test 指向正确的test.php 文件。但是我想要的是让 index.php 加载变量。所以testsite.com/helloworld 等于testsite.com/index.php?type=helloworld
    • @BITmixit 哪个部分?
    • 已修改评论。本质上,前两条线完美地工作,但我的目的并不真正需要。最后两个只是普通的旧不起作用。
    • @BITmixit:如果有效,请尝试使用此代码我会更新答案RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?food=$1
    • 已删除答案。 RewriteRule ^([^\.]+)$ index.php?type=$1 [L] 完美运行,但我不需要其余的 APACHE 代码。我的项目是否需要它,因为它是一个简单的站点,实际上只有一个 index.php 文件。感谢并为这个错误道歉(我有阅读障碍和运动障碍)
    猜你喜欢
    • 2014-07-14
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多