【问题标题】:How to add a word after my root URL for every request?如何在每个请求的根 URL 后添加一个单词?
【发布时间】:2025-11-23 23:45:01
【问题描述】:

考虑

http://localhost/MyProject/

这是根网址

访问时我想将 url 更改为

http://localhost/MyProject/Apps/

url 应该像上面一样,但应该执行

http://localhost/MyProject/index.php

那是我只想在网址中添加“应用程序”一词。没有名为“应用程序”的文件夹...

【问题讨论】:

    标签: php .htaccess url-rewriting drupal-6


    【解决方案1】:
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^localhost/MyProject/index/$ [NC]
    RewriteRule ^(.*)$ http:localhost/index.php [R=301,L]
    

    希望这会有所帮助...因为我没有在本地环境中尝试过。

    【讨论】:

    • HTTP_HOST 只会匹配域 (localhost) 而不是完整的 URI。并且 301 重定向会更改指向目的地的 URL,这是原始发布者不想做的事情,不是吗?
    【解决方案2】:

    您需要在您的 .htaccess 文件(在您的 MyProject 文件夹中)添加 Mod_Rewrite:

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteRule /? Apps/ [R=301,L] # Force people to the "Apps" folder 
      RewriteRule Apps/?$ index.php # Make "Apps" load the index
    </IfModule>
    

    【讨论】:

    • 没有Apps文件夹man..我只想在url中添加单词Apps
    • 对,这就是那条线的作用。您不希望有人在地址栏上输入http://localhost/MyProject/,而是希望他们输入http://localhost/MyProject/Apps。如果实际上他们确实输入了第一行,则第一行会为人们重写 URL。然后第二个重写规则使 http://localhost/MyProject/Apps 不会触发 404 错误(因为应用程序不存在),而是加载 index.php(但保持 URL 相同)
    • @MidnightLighting - 如果客户输入地址为localhost/MyProject 会发生什么?
    • /MyProject/MyProject/ 都应该被重定向到 /MyProject/Apps。我在第一个 RewriteRule 中添加了一个问号来捕获第一个案例以及第二个案例。
    • @MidnightLighting - 但它显示“页面未正确重定向...Firefox 检测到服务器正在以永远不会完成的方式重定向此地址的请求。”
    最近更新 更多