【问题标题】:Hide index.php AND .php from URLs while using QUERY_STRING使用 QUERY_STRING 时从 URL 中隐藏 index.php 和 .php
【发布时间】:2011-05-29 19:11:23
【问题描述】:

我希望能够在使用 QUERY_STRING 时从我的 URL 中隐藏 index.php 和 .php。

现在,我成功地在我的 .htaccess 中隐藏了 index.php(访问某些目录时除外):

RewriteEngine On
RewriteCond $1 !^(codex|_core|admin|index\.php) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]

我这样做是因为我有某些功能,例如登录/注销/注册,充当页面:example.com/login

但是,我也有一些静态页面不会从包含一堆 PHP 函数的文件中受益...例如 example.com/terms-and-conditions.php

本着统一的精神,如何在不破坏 example.com/login 的情况下将该 URL 转换为 example.com/terms-and-conditions?这甚至可能吗?我尝试添加关于 index.php 删除重写的 .php 删除重写(显然从 index.php 中删除 .php)无济于事。

我确实尝试使用位于 here 的解决方案,但它破坏了 example.com/login — 我认为是因为我使用函数/查询的方式。那里有什么我需要改变的小东西吗?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    想通了!只需要将两者结合起来,回想起来似乎很明显。 :)

    RewriteEngine On
    
    # remove .php; use THE_REQUEST to prevent infinite loops
    RewriteCond %{HTTP_HOST} ^www\.domain\.com
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
    RewriteRule (.*)\.php$ $1 [R=301]
    
    RewriteCond $1 !^(codex|_core|admin|index\.php) [NC]
    RewriteRule ^(.*)$ /index.php?/$1 [L] 
    
    # remove slash if not directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /$
    RewriteRule (.*)/ $1 [R=301]
    
    # add .php to access file, but don't redirect
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1\.php [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 2016-09-22
      • 2011-10-13
      • 2011-02-15
      • 2015-01-21
      • 1970-01-01
      • 2016-12-26
      相关资源
      最近更新 更多