Reputation: 59
From last 3 hours, I'm trying this 301 url redirect but its not working as expected. Please help me with this. here is the .htaccess file code.
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
Whenever I enter www.mydomain.com/deals/74/product-name.html, It redirects me to "www.mydomain.com/deals/74/product-name.html?id=74&name=product-name"
I'm not sure why its appending "?id=74&name=product-name" after url? I want to display only "www.mydomain.com/deals/74/product-name.html"
I don't know how to fix this problem. I'll appreciate if you can guide me on this.
Upvotes: 0
Views: 2665
Reputation: 29
I think it's because you're using (.*)/(.*)
.
This is what I always use:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [QSA,L]
</IfModule>
All URLs start checked and rewrited by PHP (or use nginx if you want speed), but .htaccess must be clean. Simple for read, simple for rewrite (to nginx or another server).
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;
}
Upvotes: 1
Reputation: 59
First of all thanks to everyone who responded and tried to help me. It took approx. the whole day to figure out myself the solution. I hope my answer will help someone,
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]
I took the last line "RewriteRule ^deals/74/product-name.html$ 74/product-name.html [R=301,L]" and pasted above all Rewrite Rules and it worked. I guess it was overlapping with some other Rewrite rules. But finally, its working as I expected.
Thanks to Everyone again. :-)
Upvotes: 0
Reputation: 785128
I'm not sure why its appending "?id=74&name=product-name" after url?
It is doing so because your URI /deals/74/product-name.html
is matching both these rules:
RewriteRule ^(.*)/(.*)\.html$ product.php?id=$1&name=$2 [L]
and
Redirect 301 /deals/74/product-name.html http://mydomain.com/74/product-name.html
It is not a good idea to mix mod_alias and mod_rewrite together. Better to use mod_rewrite itself for finer and granular control.
Your modified .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]
Upvotes: 0
Reputation: 1495
Look at the Redirect directive docs at: http://httpd.apache.org/docs/current/mod/mod_alias.html#redirect
There you can see that:
If the client requests http://example.com/service/foo.txt, it will be told to access http://foo2.example.com/service/foo.txt instead. This includes requests with GET parameters, such as http://example.com/service/foo.pl?q=23&a=42, it will be redirected to http://foo2.example.com/service/foo.pl?q=23&a=42. Note that POSTs will be discarded.
The last rule in your config is:
Redirect 301 /deals/74/product-name.html http://mydomain.com/74/product-name.html
mod_alias will append all GET parameters
If you don't want the GET parameters to be appended, maybe you might change the Redirect rule for a Rewrite rule Something like:
Rewrite ^/deals/74/product-name.html http://mydomain.com/74/product-name.html [R=301]
Easy
Upvotes: 0