2013-06-01 70 views
0

从最近3小时起,我试着使用这个301 url重定向,但它不能按预期工作。请帮我解决一下这个。这里是.htaccess文件的代码。301重定向URL附加查询字符串

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”

我不知道如何解决这个问题。如果你能指导我,我会很感激。

回答

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] 

我把最后一行的 “重写规则^交易/ 74 /产品name.html $ 74 /产品name.html [R = 301,L]” 并粘贴首先是重写规则,它工作。我想这与其他一些重写规则是重叠的。但最后,它的工作如我所料。

再次感谢大家。 :-)

1

我认为这是因为你有“饥饿的胸部” - (.*)/(.*)。适合萨莎格雷,但不适合.htaccess。

我相信.htaccess一定很干净有光泽。

喜欢我总是用:

<IfModule mod_rewrite.c> 
    RewriteEngine on 

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

所有URL开始检查和PHP rewrited(或者,如果你想用速度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.mydomain.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

+1对于Sasha和模式名称。 – goodeye

+0

lexa,感谢您的回复,但我',在LAMP服务器上使用.htaccess文件。我想在Windows服务器的web.config文件中使用“”。此外,我不想修改PHP文件只是想301重定向使用.htaccess – prototype

+0

你好lexa,谢谢你的帮助。我想出解决方案并发布了答案。顺便说它不是因为“饥饿的胸部”,胸部并不坏。大声笑;-) – prototype

0

看重定向指令文档在: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] 

+0

你好futuretelematics,感谢您的答复。我试图重写,但它不工作。 – prototype

+0

您好,先生,谢谢您的帮助。我想出解决方案并发布了答案。 – prototype

0

我不确定它为什么在URL之后追加“?id = 74 & 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] 
+0

你好anubhava,谢谢你的回复。我试过你的代码,但它不工作。它现在发现页面并且所有页面停止工作。 – prototype

+0

哪个网址不适合你? – anubhava

+0

当我上传新的.htaccess,所有的网址停止工作 – prototype

相关问题