【问题标题】:htaccess help, removing a string from the URLhtaccess 帮助,从 URL 中删除一个字符串
【发布时间】:2011-04-23 17:29:40
【问题描述】:

我想知道,是否可以从 URL 中删除 index.php?基本上在网站的某些页面上,我有这种结构,

http://www.domain.com/index.php/members/register,但其他页面我有这样的 URL 结构,http://www.domain.com/category/products/id/5,我想知道 htaccess 是否可以在需要时删除 index.php 和任何属性斜杠?我该怎么做呢?

【问题讨论】:

    标签: apache .htaccess mod-rewrite server-configuration


    【解决方案1】:

    是的,你可以。使用此规则,任何请求的 /index.php 都将被删除:

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php[/?\ ]
    RewriteRule ^index\.php(/(.*))?$ /$2 [L,R=301]
    

    但您最好从一开始就使用正确的 URL,以便您的应用程序提供链接不包含 /index.php 的文档。

    【讨论】:

      【解决方案2】:

      如果要全局重写 index.php/controller/action

      这个 .htaccess 配置应该可以解决问题:

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

      此配置在 Apache 上检查文件/目录是否存在于磁盘上(即请求与磁盘上的真实资源匹配),并在需要时将请求重写为 your front controller

      所以http://www.domain.com/resources/image.png 应该返回图片资源。 而http://www.domain.com/user/show/5 应该透明地重写为http://www.domain.com/index.php/user/show/5

      使用此配置,您可以删除应用程序 URL 中的所有 index.php 引用,并将重写留给 Web 服务器。

      【讨论】:

        猜你喜欢
        • 2014-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多