.htaccess:如果没有查询字符串,则删除url中的index.php

时间:2014-08-21 14:12:49

标签: .htaccess mod-rewrite indexing

如果没有查询字符串,我想删除网址中的index.php。

我的.htaccess是:

RewriteCond %{QUERY_STRING} ^$
RewriteRule index.php http://www.domain.fr [R=301,L]

RewriteRule ^(.*)/(.*)/$ index.php?category=$1&region=$2 [L,QSA]

www.domain.fr/index.php =>好的,重定向到www.domain.fr

www.domain.fr/index.php?i=1 =>好的,没有重定向

www.domain.fr/category/region/(如果在网址中手动输入)=>好的,没有重定向

www.domain.fr/category/region/(如果通过php表单发布)=>重定向(并且不应该)到www.domain.fr

这是我的php表单:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <select id="category" name="category">..</select>
  <input id="send" name="send" type="submit" value="search" />
</form>

以及此表单的代码(位于页面顶部):

if(isset($_POST['send'])){
    $category = $_POST['category']; 
    $region = $_POST['region'];
    header('Location: http://www.domain.fr/$category/$region/');
    exit;
}

如果我删除cond RewriteCond%{QUERY_STRING} ^ $,php表单重定向到www.domain.fr/category/region,但是如果我把cond重定向到www.domain.fr,

任何想法?

2 个答案:

答案 0 :(得分:0)

您的规则顺序是问题,请遵守以下规则:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^domain\.fr$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,NE,L]

RewriteCond %{THE_REQUEST} /index\.php[^?] [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ / [R=302,L]

RewriteRule [A-Z] ${lc:%{REQUEST_URI}} [R=302,L]

RewriteRule .* - [E=SD:${vhost:%{HTTP_HOST}}]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ index.php?category=$1&region=$2 [L,QSA]

RewriteRule ^([^/]+)/?$ index.php?category=$1 [L,QSA]

答案 1 :(得分:0)

Anubhava我找到了答案!!!

问题只是关于php表单,需要删除:

<?php echo $_SERVER['PHP_SELF'];?>

解决方案很简单:

<form method="post" action="">

但是非常感谢Anubhava!