Redirect dynamic url to home page - .htaccess

I want to redirect dynamic URL to homepage, URL has 4 parameters
http://example.com/abc.php?slug=Adjective&getId=2&sub_id=1&inner_id=67

To redirect the dynamic URL to homepage , you can use the following rule,
RewriteEngine on
RewriteCond ℅{THE_REQUEST} /abc.php\?slug=Adjective&getId=2&sub_id=1&inner_id=67\s [NC]
RewriteRule ^ /? [L,R]
This will redirect http://abc.php?slug=Adjective&getId=2&sub_id=1&inner_id=67 to http://example.com/ .
Do not remove the question mark ? from the Rewrite destination as this discards the old querystring.

Related

Redirect (301 or 302) for translated page not working

I want to redirect a translated page to another translated page in the htaccess file.
In the original language it works (Redirect 301 /helpie_faq/what-are-my-benefits/ /faq/#hfaq-post-1683), but the redirection of translated pages doesn't work, e.g.
Redirect 301 /helpie_faq/welche-vorteile-erhalte-ich-als-neueinwanderer/?lang=de /faq/?lang=de#hfaq-post-3001
How can I make this work out?
You need to match on the query string, which the redirect directive does not support. Instead you should use RewriteCond on the URI (REQUEST_URI) and on the query string (QUERY_STRING) and a matching RewriteRule.
RewriteEngine On
RewriteCond %{REQUEST_URI} /helpie_faq/welche-vorteile-erhalte-ich-als-neueinwanderer$
RewriteCond %{QUERY_STRING} ^lang=de$
RewriteRule ^.*$ /faq/?lang=de#hfaq-post-3001 [L,R=301]
For more examples look here Apache Redirect 301 fails when using GET parameters, such as ?blah= or here Redirecting URLs (with specific GET parameters)

Redirect to a url if a custom url parameter appear

how can i redirect a url to other url with selected url parameter using htacess
that is if customer enter a url like
http://exmaple.com/?par_1=%string&par_2=%string&par_3=%string
then i want to remove url parameter 1 and 3 and redirect with par_2 value .
That is
http://exmaple.com/?par_2=%string.
How it can be done ? Here % string will be anything .
Please help
You can use something like the following
RewriteEngine on
RewriteCond %{THE_REQUEST} /\?par_1=([^&]+)&par_2=([^&]+)&par_3=([^&\s]+) [NC]
RewriteRule ^ http://exmaple.com/?par_2=%2 [L,R]

Redirecting SEO rewritten url

I'm using these rewrite rules to go from dynamic urls to static ones.
RewriteEngine On
RewriteRule ^songs/album/([^/]*)\.html$ /index.html?album=$1 [L,QSA]
Old url: http://example.com/index.html?album=rockstar
New url: http://example.com/songs/album/rockstar.html
But if I try to redirect old url to new url, it doesn't work
RedirectMatch 301 ^/index.html?album=(.*)\$ http://example.com/songs/album/$1.html
Any ideas?
The RedirectMatch directive in mod_alias won't match against the query string, only the /index.html part. You'd need to use a RewriteCond ${QUERY_STRING} <regexp> to match against it. But if you redirect like that you're going to cause a loop because the URI is put through the rewrite engine until the URI comes out unchanged:
Person types http://example.com/index.html?album=rockstar into their URL address bar
browser gets redirected to http://example.com/songs/album/rockstar.html
mod rewrite sees /songs/album/rockstar.html and rewrites it to /index.html?album=rockstar
the redirect rule successfully matches against /index.html?album=rockstar
repeat from step 2.
You need to make sure to only redirect if the actual request was for /index.html?album=rockstar, and not when it's been through the rewrite engine:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\?album=(.+)\ HTTP
RewriteRule ^index\.html$ http://example.com/songs/album/%1.html? [R=301,L]
The %{THE_REQUEST} is the actual HTTP request, and not the rewritten URI. The %1 is a backreference to a match in a previous RewriteCond and the ? at the end of the redirect URL tells the RewriteRule not to append the query string to the end.

301 Redirects problem - urls with ? and =

I'm new with 301 redirects through .htacces.
I can get simple redirects as
redirect 301 /test.html http://www.domain.com/test2.html
to work but I have some urls like this
redirect 301 /test.asp?Group=100 http://www.domain.com/test3.html
and for some reason these don't work.
Thanks.
Here is set of rules for URLs you have provided:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =group=113 [NC]
RewriteRule ^group\.asp$ http://domain.dk/til-born.htm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} =product=1136 [NC]
RewriteRule ^product\.asp$ http://www.domain.dk/til-born/bukser.html? [NC,R=301,L]
As you can see query string is matched separately to the page name. So .. for each of such redirects you need 2 lines: RewriteCond & RewriteRule.
The rule above will do EXACT match, which means /group.asp?group=113&param=value will not be redirected because query string is group=113&param=value which is more than just group=113.
To have such redirect working (when there are some optional parameters in query string) you have to modify it: RewriteCond %{QUERY_STRING} (^|&)group=113(&|$) [NC] -- this will match group=133 anywhere in query string (group=113 and group=11366 are still different, so no problems here).
This needs to be placed in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
The Redirect directive (as far as I know) matches only on the path, not querystring. Instead, use RewriteRule. The QSA instructs the rewrite engine to append the querystring onto the new redirected URL.
RewriteEngine On
RewriteRule ^test\.asp http://www.domain.com/test3.html [L,R=301,QSA]

Redirect pages from old to new site - one by one

I'm trying to redirect pages from several old domains on one new domain on a page-to-page basis, that is:
page 1: http://www.example.org/default.asp?id=1 redirects to http://www.example2.org/newpage.html
page 2: http://www.example2.org/default.asp?id=2 redirects to http://www.example.org/contact.html
...and so on. Every old page will redirect to a specific new page.
I have tried to write something in .htaccess along the lines of
Redirect 301 http://www.example.org/default.asp?id=1 h-tp://www.example.org/newpage.html
Redirect 301 http://www.example2.org/default.asp?id=2 h-tp://www.example.org/contact.html
But no luck so far. mod_alias is enabled...
I also tried using RewriteCond and RewriteRule like this:
RewriteCond %{HTTP_HOST} example.org
RewriteRule default.asp?id=1 http://www.example.org/newpage.html [NC,L,R=301]
...
In other words: I want to do a page-by-page redirect from id-based to alias-based along with a merge of three sites/domains into one site.
I hope there is a useful solution. Thanks in advance!
The Redirect directive does only work with the URL path but not the host or query of the URL.
But it is possible with mod_rewrite:
RewriteCond %{HTTP_HOST} =example.org
RewriteCond %{QUERY_STRING} =id=1
RewriteRule ^default\.asp$ http://www.example.org/newpage.html [NC,L,R=301]
And as already said in the comments, you can use a rewrite map for the ID-to-alias mapping:
1 foo-page
2 bar-page
3 baz-page
…
The RewriteMap declaration (here a simple plain text file):
RewriteMap id-to-alias txt:/absolute/file/system/path/to/id-to-alias.txt
And finally the application:
RewriteCond %{HTTP_HOST} =example.org
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&?([^&].*)?$
RewriteCond ${id-to-alias:%3}&%1%4 ^([^&]+)&(.*)
RewriteRule ^default\.asp$ http://www.example.org/%1.html?%2 [NC,L,R=301]
That should also preserver the remaining query. If you don’t want that:
RewriteCond %{HTTP_HOST} =example.org
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&?([^&].*)?$
RewriteCond ${id-to-alias:%3} .+
RewriteRule ^default\.asp$ http://www.example.org/%0.html? [NC,L,R=301]
A good alternative to mod_rewrite is to use whatever language you are most comfortable with and put the logic in your script. Point all URLs you'd like to redirect to your simple front controller.
RewriteRule ^default.asp$ /redirect.php [L]
RewriteRule ^another/path/to_some_file.ext$ /redirect.php [L]
And then, in redirect.php:
<?php
if ($_SERVER['SCRIPT_URL'] == '/default.asp'){
$map = array(
1 => '/newpage.html',
2 => '/contact.html'
);
if (isset($_GET['id'])){
$id = $_GET['id'];
if (isset($map[$id])){
header('HTTP/1.1 301 Moved Permanently', true, 301);
header('Location: http://'.$_SERVER['HTTP_HOST'].$map[$id]);
exit;
}
}
}
// ... handle another/path/to_some_file.ext here, or other domains, or whatever ...
// If we get here, it's an invalid URL
header('HTTP/1.1 404 Not Found', true, 404);
echo '<h1>HTTP/1.1 404 Not Found</h1>';
echo '<p>The page you requested could not be found.</p>';
exit;
?>
Just adding some more info
The OP's redirect directives are probably in the wrong format - from the relevant apache doc page:
The Redirect directive maps an old URL
into a new one by asking the client to
refetch the resource at the new
location.
The old URL-path is a case-sensitive
(%-decoded) path beginning with a
slash. A relative path is not
allowed. The new URL should be an
absolute URL beginning with a scheme
and hostname. Example:
Redirect /service http://foo2.bar.com/service
So that would make it:
Redirect 301 /default.asp?id=1 http://www.example.org/newpage.html
Hope that helps
In httpd.conf:
RedirectPermananent URL1 URL2

Resources