【问题标题】:remove index.php and index.php query string from url从 url 中删除 index.php 和 index.php 查询字符串
【发布时间】:2013-06-29 12:14:10
【问题描述】:

我遇到以下问题: 我有这个网址 www.mysite.com/inde.php?SomePageName 我想将其重写为 www.mysite.com/SomePageName 另外,我想在没有任何参数的情况下调用 url 时删除 index.php,例如:mysite.com/index.php 或 mysite.com/index.php?应该重定向到 mysite.com/

我的 htaccess 文件如下所示:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%2? [L,R=302]
RewriteRule ^index.php$ http://www.mysite.com/ [R=302,L]

由于最后一个声明,它现在正在循环。

有什么方法可以实现吗?

谢谢

PS:整个htaccess

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^cumpara-(.*)-(.*)\.html$ index.php?Profile&ID=$2 [L]
RewriteRule ^lista-scut-motor-metalic-(.*)-(.*)-(.*)\.html$ index.php?List&SubID=$3&Categorie=$1&Subcategorie=$2 [L]
RewriteRule ^scut-motor-metalic-(.*)-(.*)\.html$ index.php?ViewCat&CatID=$2 [L]
RewriteRule ^foto-(.*)-(.*).jpg$ includes/timthumb.php?src=data/$1/$2.jpg&w=300&q=100 [L]
RewriteRule ^product-(.*)-(.*).jpg$ includes/timthumb.php?src=data/$1/$2.jpg&w=530&q=100 [L]
RewriteRule ^side-(.*)-(.*).jpg$ includes/timthumb.php?src=data/$1/$2.jpg&w=175&h=110&q=100 [L]
RewriteRule ^zoom-(.*)-(.*).jpg$ data/$1/$2.jpg [L]

RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^.*$ http://www.mysite.com/%1/? [R=302,L]

RewriteRule Despre index.php?Despre [L]
RewriteRule Help index.php?Help [L]
RewriteRule Livrare index.php?Livrare [L]
RewriteRule Contact index.php?Contact [L]

RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mysite\.com\/$1" [R=301,L]

ErrorDocument 404 /404.html

【问题讨论】:

    标签: .htaccess mod-rewrite query-string


    【解决方案1】:

    如果是在我之后,最简单的解决方案是通过 PHP 处理 index.php 的副本

    拥有这个 .htaccess:

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

    您将所有 REQUEST_URI 写入 PHP,因此,检查 index.php 是否存在,如果存在,则重定向到非 index.php 链接:D 像这样:

    if( strpos($_SERVER['REQUEST_URI'], "?") !== FALSE ) {
        $checkq = explode("?", $_SERVER['REQUEST_URI']);
        $vars = "?".$checkq[1];
        $checkq = $checkq[0]; 
    }else { 
        $checkq = $_SERVER['REQUEST_URI'];
        $vars = ''; 
    }
    if( strpos($checkq, "/") !== FALSE ) {
        $link = explode("/", $checkq);
    
        if($link[1] == "index.php"){
            unset( $link[1] );
            $link = implode("/",$link);
    
            header('Location: http://'.$_SERVER['HTTP_HOST'].$link.$vars);
            exit();
            }
    }
    

    即使您使用 GET 变量/您的自定义 .htaccess,这也应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 2016-05-14
      • 2014-10-28
      • 2012-06-15
      • 2011-05-20
      • 2012-09-21
      相关资源
      最近更新 更多