301 Redirect URL Appending Query String - php

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.

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;
}

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

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]

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. :-)

Related

.htaccess-The page isn't redirecting properly

I am trying to redirect index.html to home.html and also remove that home.html from url via .htaccess. But it shows The page isn't redirecting properly.
Which Means i need to redirect mydomain.com/index.html to mydomain.com/home.html and I need to show mydomain.com/ only when I visit index page.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Redirect 301 "/index.html" "/home.html"
RewriteCond %{THE_REQUEST} ^GET.*home\.html [NC]
RewriteRule (.*?)home\.html/*(.*) /$1$2 [R=301,NE,L]
</IfModule>
What you could try instead of utilizing mod_rewrite is setting DirectoryIndex to the desired page (in your case home.html):
DirectoryIndex home.html
So, if you're calling domain.com/ it should give you the contents of domain.com/home.html
For reference, the Apache configuration manual:
https://httpd.apache.org/docs/2.4/mod/mod_dir.html
"DirectoryIndex Directive"
Referencing the conversation right below:
Maybe this could work (Sorry, I cant test it right now)
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} =/index.html
RewriteRule (.*?)home\.html/*(.*) /$1$2 [R=301,NE,L]
A bit of explanation: If the requested host matches example.com and the called URI is in fact not a real file or folder plus the URI equals to
"index.html" it should redirect to home.html.
EDIT: As you dont want to show the home.html we can maybe get away by "proxying" the request internally instead of forcing an external redirect. Try to replace "R=301" in the last line (the RewriteRule) with "P" so it says RewriteRule (.*?)home\.html/*(.*) /$1$2 [P,NE,L]
// It's working code i hope it's useful ..
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 / http://newsite.com/
</IfModule>
or
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?http://dbdenterprise\.in$
RewriteRule ^(.*)$ http://dbdenterprise.com/$1 [R=301,QSA,L]

Rewrite rule based on QUERY_STRING

I am trying to redirect the url
www.mydomain.com/index.php?route=first/second to www.mydomain.com/second
I have tried the following rules but none seem to work
RewriteEngine On
1) RewriteRule ^index.php?route=first/second$ /second [R=301,L]
2) RewriteRule ^index.php?route=first/second$ www.mydomain.com/second [R=301,L]
3) Redirect ^index.php?route=first/second /second/
4) Redirect ^index.php?route=first/second www.mydomain.com/second/
5) Redirect ^www.mydomain.com/index.php?route=first/second /second/
6) Redirect ^www.mydomain.com/index.php?route=first/second www.mydomain.com/second/
However the below rule works:
RewriteRule ^second$ /index.php?route=first/second [R=301,L]
but point 1 and 2 above does not work
Is there any solution to this?
make sure you put RewriteEngine On before any of the rules.
Make sure: index.php exists, else for that we have to set some condition for file existence as well like RewriteCond %{REQUEST_FILENAME} !-f
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} route=first/second
RewriteRule .* /second? [R,L]

htaccess doesn't work as expected

I have an htaccess rewrite URL as below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mywebsite.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^my-page\.html$ /my-page.php [L]
RewriteRule ^my-page/([^/]*)\.html$ /level1.php?num=$1 [L]
RewriteRule ^my-page/([^/]*)/([^/]*)\.html$ /level2.php?level1=$1&level2=$2 [L]
RewriteRule ^([^/]*)\.html$ /level3.php?level3=$1 [L]
These rules above rewrite URLs from mywebsite.com/my-page.php to mywebsite.com/my-page.html.
Now, what I want to achieve is mywebsite.com/my-page/ to be redirected to mywebsite.com/my-page.php (which in turn rewrites to mywebsite.com/my-page.html).
What I have tried, I created a directory "my-page" and tried to redirect requests from mywebsite.com/my-page/ to /my-page.html.
I don't know what went wrong. I can see in the network tab that a request is made to /my-page/ and gets rewritten to mywebsite.com/my-page.htmlmy-page/, which gives a 302 Status ☹
Please help! Thank you.
You can try use RedirectMatch to achieve this.
Redirect to my-page.php:
RedirectMatch 301 ^/my-page/ http://mywebsite.com/my-page.php
or straight away to my-page.html if this is your goal:
RedirectMatch 301 ^/my-page/ http://mywebsite.com/my-page.html
or, what will be best - change the code responsible for mywebsite.com/my-page.htmlmy-page/, but I can't see it in question you have asked :)
Please give the following a try. Brief descriptions are found in the comments for each section.
RewriteEngine On
# Trim www.
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R=301,L]
RewriteBase /
# Redirect /my-page[/] to /my-page.html
# >> Note: change 302 to 301 to make permanent
RewriteRule ^my-page/?$ my-page.html [R=302,L]
# Allow existing files and directories
# Recommended to comment out the first line
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Rewrite *.html to respective page
RewriteRule ^my-page.html$ my-page.php [L]
RewriteRule ^my-page/([^/]*).html$ level1.php?num=$1 [L]
RewriteRule ^my-page/([^/]*)/([^/]*).html$ level2.php?level1=$1&level2=$2 [L]
RewriteRule ^([^/]*).html$ level3.php?level3=$1 [L]
The important part here is that you do the required redirect before any other rewrites (except the www. removal).
Also, you previously had the two conditions which stated that if the request was not for a file or directory, then proceed with the next rule, but that wouldn't have accounted for the last two rules. As such, this version tells Apache to stop everything if the request is for an existing file or directory. I would recommend, for security purposes, that you comment out the line that checks for existing directories.

Redirect 301 of .htaccess didn't redirect correctly

My redirection in my .htacces doesn't want redirect correct url.
DefaultLanguage fr-FR
Options -Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^clic_go\.php\?id=(.+)$ /clic.php?id=$1 [R=301,L]
He redirect to :
/?id=4
i want :
/clic.php?id=4
Any idea ?
Thanks you =D
Use:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)id=(.+)(?:&|$) [NC]
RewriteRule ^clic_go\.php$ /clic.php?id=%1 [NC,R=301,L]
Because query string is not in the RewriteRule url.
With all the query string, you can just do:
RewriteEngine on
RewriteRule ^clic_go\.php$ /clic.php [NC,R=301,L]
Because without addition, it is copied

RewriteCond redirect a domain to another except for one script, do the opposite

1) in general, I want everything on www.ABC.com to be redirected to www.XYZ.com
2) EXCEPT when it's
www.ABC.com/this/123([a-z]+).html
... it must Rewrite (NOT redirect) to ...
www.ABC.com/that_script.php?var=123
3) Also EXCEPT: when it's
www.XYZ.com/this/123([a-z]+).html
... it must go (redirect) to ....
www.ABC.com/this/123([a-z]+).html
(so the 2nd rule will apply after that)
EDIT Both domains parked on the same hosting, so sharing the same HTACCESS
EDIT2 Language of the project is PHP
I tried various RewriteCond with %{REQUEST_URI} or %{SCRIPT_FILENAME} but it never works, either saying it's an infinite loop or simply don't take the condition at all.
EDIT3 In PHP, it would looks like that...
if( FALSE !== strstr($_SERVER['HTTP_HOST'], 'ABC.com') && FALSE !== strstr($_SERVER['SCRIPT_FILENAME'], 'that_script') ) {
header("Location: http://www.XYZ.com".$_SERVER['REQUEST_URI'],TRUE,301);
}
if( FALSE !== strstr($_SERVER['HTTP_HOST'], 'XYZ.com') && FALSE === strstr($_SERVER['SCRIPT_FILENAME'], 'that_script') ) {
header("Location: http://www.ABC.com".$_SERVER['REQUEST_URI'],TRUE,301);
}
I want this, but in HTACCESS
Based on what you have above, it will be something to the effect of:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
ReWriteRule ^/this/([a-z0-9]+).html www.ABC.com/that_script.php?var=$1 [PT,L]
RewriteCond %{HTTP_HOST} www.ABC.com$ [NC]
ReWriteRule ^(.*)$ www.XYZ.com [R=301,L]
</IfModule>
This will do the following -
1 - Any traffic hitting http://www.ABC.com/this/<Anything made of Numbers and Letters> will pass through to http://www.ABC.com/that_script.php?var=<Anything made of Numbers and Letters> while continuing to say http://www.ABC.com/this/<Anything made of Numbers and Letters> to the visitor.
2 - Any traffic hitting anything other than what is referenced to #1 will be redirected to www.XYZ.com with a HTTP code of 301 (Moved Permanently).
Remember that you have to be able to put mod_rewrite rules in your .htaccess files. Having the option of AllowOverride FileInfo for the directory would make sure of that.
Use this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Redirect www.xyz.com/this/123([a-z]+).html to www.abc.com/that_script.php?var=123
RewriteCond %{HTTP_HOST} ^(www\.)?xyz\.com$ [NC]
RewriteRule ^this/(123)[a-z]+\.html$ http://www.abc.com/that_script.php?var=$1 [R,L,NC]
# Forward www.abc.com/this/123([a-z]+).html to www.abc.com/that_script.php?var=123
RewriteCond %{HTTP_HOST} ^(www\.)?abc\.com$ [NC]
RewriteRule ^this/(123)[a-z]+\.html$ that_script.php?var=$1 [L,QSA,NC]
# Redirect abc.com to www.xyz.com
RewriteCond %{HTTP_HOST} ^(www\.)?abc\.com$ [NC]
RewriteRule ^ http://www.xyz.com%{REQUEST_URI} [R,L]
Change all R to R=301 once you have verified that it is all working fine.
Also note that I have used first RewriteRule in a way to avoid 1 extra forward (RewriteRule # 2 above).
Have you read the official documentation about mod_rewrite ? All the information you need is in the manual, there is no secret.
RewriteEngine On
RewriteBase /
# Redirect www.xyz.com/this/123([a-z]+).html to www.abc.com/this/123([a-z]+).html.
RewriteCond %{HTTP_HOST} ^www.xyz.com$ [AND]
RewriteCond %{REQUEST_URI} ^/this/123([a-z]+).html$
RewriteRule ^(.*)$ http://www.abc.com/$1 [R=301,L]
# Rewrite www.abc.com/this/123([a-z]+).html to www.abc.com/that_script.php?var=123.
RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
RewriteRule ^/this/123([a-z]+)\.html$ /that_script.php?var=123 [L]
# Redirect everything else to www.xyz.com.
RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
RewriteRule ^(.*)$ http://www.xyz.com/$1 [R=301,L]

Categories