htaccess重定向与GET请求

时间:2014-01-05 18:39:20

标签: php apache .htaccess mod-rewrite redirect

以下是我的htaccess:

Options -Multiviews -Indexes
RewriteEngine on
RewriteBase /

## don't touch /Forum URIs
RewriteRule ^Forum/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?seo=([^&\s]+) [NC] 
RewriteRule ^ %1/%2? [R,L]

# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?seo=$2 [L,QSA] 

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]

以上代码工作正常,但以下两种情况除外:

    对于“论坛”目录下的页面,
  1. .php扩展名未被删除。他们在这些页面中没有GET请求。如果我删除了上述论坛的条件,则会发现404错误论坛/ .php未找到。

  2. 此外,对于GET请求变量不是'seo'的页面,不会删除.php扩展名。

  3. 带有'seo'作为GET变量的网址正在转换为SEO友好,但GET请求与其他变量如'id'或'rg =& vp =& da ='不起作用。我已经添加了相同但仍然没有影响的附加规则(对于seo)。

  4. 请告知。

1 个答案:

答案 0 :(得分:1)

让你的.htaccess像这样:

Options -Multiviews -Indexes
RewriteEngine on
RewriteBase /

## hide .php extension snippet

# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} \s(.+?)\.php\?seo=([^&\s]+)(?:&(\S+))? [NC] 
RewriteRule ^ %1/%2?%3 [R,L]

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s(.+?)\.php(\S*) [NC]
RewriteRule ^ %1%2 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?seo=$2 [L,QSA]