Rewrite rule based on QUERY_STRING - php

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]

Related

Redirect one page to another with parameters via htaccess

I want to redirect from a page to another with parameters with htaccess.
For example:
From: www.websitename.co.uk/mylink.php?u=*username*
To: www.websitename.co.uk/mylink/*username*
I tried this but it's not working.
RewriteEngine on
RewriteRule www.websitename.co.uk/mylink.php?u=*username* www.websitename.co.uk/mylink/*username* [R=301]
Where am I doing wrong?
Working fine for me , hope this will work fine.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/mylink\.php$
RewriteCond %{QUERY_STRING} u=([a-zA-Z]+)
RewriteRule .* http://websitename.co.uk/mylink/%1? [R=301]
Rewrite rules use regex like this:
RewriteEngine on
RewriteRule ^/mylink.php?u=(.*) /mylink/$1 [R=301]
Reference: https://httpd.apache.org/docs/current/rewrite/intro.html

.htaccess; Too many redirects if two different redirects in .htaccess

I've two types of URLs:
http://www.example.com/?content=contact_form.php
and
http://www.example.com/?content=product.php&id=20
I changed my whole URL system like this:
http://www.example.com/file/contact_form
and
http://www.example.com/product/I-m-the-title/20
Of course I made 301 redirect with .htaccess to tell Google and co. the new URL.
I made it like this:
# Rewrite URLs
RewriteEngine On
RewriteRule ^(ignore)($|/) - [L]
RewriteRule ^file/([^/]*)$ /?content=$1.php [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /?content=$1.php&title=$2&id=$3 [L]
# Redirect old URL to new URL
RewriteCond %{QUERY_STRING} ^content=contact\_form\.php$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://www.example.com/file/contact_form? [R=301,L]
RewriteCond %{QUERY_STRING} ^content=product\.php&class=I-m-the-title$
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule .* http://www.example.com/I-m-the-title/Test/20? [R=301,L]
My problem:
It's perfectly working for: http://www.example.com/?content=product.php&id=20
But for http://www.example.com/?content=contact_form.php I'm getting the message that it couldn't get opened because of too much redirect.
Does anybody know what I'm doing wrong? I hope anybody can help me soon because I have to fix it before Google misinterprets it.
Your rule cause an infinite loop because it is rewriting your uri to the same location again and again overriding your internal and external redirects.. To fix the Rewrite loop, add the following at the top of your htaccess
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [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.

301 Redirect URL Appending Query String

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

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