从url中删除index.php和index.php查询字符串

时间:2013-07-02 07:55:53

标签: .htaccess mod-rewrite query-string

我遇到以下问题: 我有这个网址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

1 个答案:

答案 0 :(得分: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,这也应该可以。