[英]301 Redirect URL Appending Query String
从最近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”
我不确定为什么在网址后附加“?id = 74&name = product-name”? 我只想显示“ www.mydomain.com/deals/74/product-name.html”
我不知道如何解决这个问题。 如果您能指导我,我将不胜感激。
我认为这是因为您正在使用(.*)/(.*)
。
这就是我一直使用的:
<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;
}
在以下位置查看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参数,则可以将Redirect规则更改为Rewrite规则,例如:
Rewrite ^/deals/74/product-name.html http://mydomain.com/74/product-name.html [R=301]
简单
我不确定为什么在网址后附加“?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]
首先,感谢所有响应并试图帮助我的人。 花了大约。 一整天来弄清楚自己的解决方案。 希望我的回答能对某人有所帮助,
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]”,并粘贴在所有“重写规则”之上,并且它起作用了。 我猜想它与其他一些重写规则重叠。 但最后,它的工作符合我的预期。
再次感谢大家。 :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.