301重定向URL附加查询字符串

时间:2013-06-01 18:12:23

标签: php apache .htaccess redirect url-rewriting

从最近3个小时开始,我正在尝试这个301网址重定向,但它没有按预期工作。请帮我解决一下这个。这是.htaccess文件代码。

Options +FollowSymLinks +SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteBase /
rewriterule ^(.*)/(.*)\.html$ product.php?id=$1&name=$2 [L]
rewriterule ^deals/(.*)$ details.php?id=$1 [L]
rewritecond %{SERVER_PORT} 80
rewritecond %{REQUEST_URI} publisher.php

Redirect 301 /deals/74/product-name.html http://mydomain.com/74/product-name.html

每当我输入www.mydomain.com/deals/74/product-name.html时,它会将我重定向到“www.mydomain.com/deals/74/product-name.html?id=74&name=product -name“

我不确定为什么在url之后附加“?id = 74& name = product-name”?我只想显示“www.mydomain.com/deals/74/product-name.html”

我不知道如何解决这个问题。如果你可以指导我,我将不胜感激。

4 个答案:

答案 0 :(得分:1)

我认为这是因为你正在使用(.*)/(.*)

这就是我经常使用的:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [QSA,L]
</IfModule>

所有URL都由PHP开始检查和重写(如果你想要速度,可以使用nginx),但.htaccess必须干净。读取简单,重写简单(到nginx或其他服务器)。

的index.php:

if (substr($_SERVER['HTTP_HOST'], 0, 4) != 'www.'){
    $protocol = (substr(PHP_SAPI, 0, 3) == 'cgi' ? 'Status:' : $_SERVER['SERVER_PROTOCOL']);

    header($protocol.' 301 Moved Permanently');
    header('Location: http://www.example.com'.$_SERVER['REQUEST_URI']);

    exit;
}

// Simple path checker
$uri = trim($_SERVER['REQUEST_URI'], '/');
$path = pathinfo($uri);

// Check

if ($path['extension'] == '.html'){
    $_GET['id'] = $path['dirname'];
    $_GET['name'] = $path['filename'];

    include 'product.php';
    exit;
}

if ($path['dirname'] == 'deals'){
    $_GET['id'] = $path['filename']; 

    include 'details.php';
    exit;
}

答案 1 :(得分:0)

查看Redirect指令文档:http://httpd.apache.org/docs/current/mod/mod_alias.html#redirect

你可以看到:

  

如果客户请求http://example.com/service/foo.txt,则会被告知要访问   而是http://foo2.example.com/service/foo.txt。这包括使用GET参数的请求,   例如http://example.com/service/foo.pl?q=23&a=42,它将被重定向到   http://foo2.example.com/service/foo.pl?q=23&a=42。请注意,POST将被丢弃。

配置中的最后一条规则是:

Redirect 301 /deals/74/product-name.html http://mydomain.com/74/product-name.html

mod_alias将附加所有GET参数

如果您不想追加GET参数,可能您可能会更改重写规则的重定向规则 类似的东西:

Rewrite ^/deals/74/product-name.html http://mydomain.com/74/product-name.html [R=301]

轻松

答案 2 :(得分:0)

  

我不确定为什么在url之后附加“?id = 74&amp; name = product-name”?

这样做是因为您的URI /deals/74/product-name.html符合以下规则:

RewriteRule ^(.*)/(.*)\.html$ product.php?id=$1&name=$2 [L]

Redirect 301 /deals/74/product-name.html http://mydomain.com/74/product-name.html

mod_alias和mod_rewrite混合在一起并不是一个好主意。最好使用mod_rewrite本身进行更细致的控制。

您修改后的.htaccess:

Options +FollowSymLinks +SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
RewriteBase /

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

RewriteRule ^deals/(74/product-name\.html)$ /$1 [L,R=301,NC]

RewriteCond %{REQUEST_URI} !/74/ [NC]
RewriteRule ^([^/]+)/([^.]+)\.html$ /product.php?id=$1&name=$2 [L]

RewriteRule ^deals/(.*)$ /details.php?id=$1 [L]

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} /publisher.php [L]

答案 3 :(得分:0)

首先感谢所有回复并试图帮助我的人。花了大概。整整一天弄清楚自己的解决方案。我希望我的回答能帮助别人,

Options +FollowSymLinks +SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^deals/74/product-name.html$ 74/product-name.html [R=301,L]

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

RewriteRule ^(.*)/(.*)\.html$ product.php?id=$1&name=$2


rewritecond %{SERVER_PORT} 80
rewritecond %{REQUEST_URI} publisher.php
RewriteRule ^(.*)$ publisher.php [L]

我把最后一行“RewriteRule ^ deals / 74 / product-name.html $ 74 / product-name.html [R = 301,L]”并粘贴在所有重写规则之上并且它有效。我猜它与其他一些Rewrite规则重叠。但最后,它按照我的预期工作。

再次感谢大家。 : - )

相关问题