How to redirect a specific page to home page in .htaccess? - apache

I want to redirect a specific page to home page.
My source URL is http://examlple.com/index.html?page=22
My target URL is http://examlple.com/
I have tried Redirect /index.html?page=22 http://example.com/
also I have tried by using 301 and RewriteRule but no success yet, this is not working.

You can't match against querystrings in Redirect directive.
Try mod_rewrite
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=22$
RewriteRule ^index\.html$ http://example.com/? [NC,L,R]
Empty question ? mark at the end is important as it will discard the original query string from url.

Related

how to redirect url using custom logic in .htaccess?

I want to redirect url using htaccess file from project root folder.
I can easily redirect url like www.xyz.com/ar/contact to www.xyz.com/contact using this
Redirect 301 /ar/contact/ /contact/
but how to redirect url like this
www.xyz.com/ar/shop/xyz.html to www.xyz.com/shop/xyz.html
I have tried this
Redirect 301 /ar/shop/^(.*)$ /shop/$1
Redirect /ar/shop/^(.*)$ /shop/$1 [R=301,L]
Redirect ^/ar/shop/(.*) /shop/$1
but it did't work.
Could you please try following. Written and tested with shown samples only.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/ar/(.*)/? [NC]
RewriteRule ^(.*) /%1 [R=301,NE,L]
You can not use a regex based pattern in Redirect directive. If you want to redirect everything that comes after /ar/shop/ to /shop/ then just use a static pattern /ar/shop/ .
Redirect 301 /ar/shop/ /shop/
This will redirect all URIs starting with /ar/shop/ to /shop/ . forexample /ar/shop/foobar will get redirected to /shop/foobar > .

Stop .htaccess redirect with query string

My site uses an .htacess file to redirect the root domain name to another URL. The current redirect looks like this:
RewriteEngine on
RewriteRule ^$ http://example2.com/ [R=301,L]
I only want the root domain to be redirected (example1.com to example2.com) so the above works correctly. However if a query string is used within the old URL. For example
http://example1.com/?pages=3
The redirect still works which I don't want. Im just having the redirect problem when a query string is used, anything else is fine:
http://example1.com/ - Does Redirect ✓
http://example1.com/home - Doesn't redirect ✓
http://example1.com/?page=3 - Does Redirect X - need this not to redirect
Can anyone help?
You can put a ? at the end of the destination to discard old querystring :
RewriteEngine on
RewriteRule ^$ http://example2.com/? [R=301,L]
If you dont want to redirect when the request contains query string, you may use :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ http://example2.com/ [R=301,L]

301 Redirect not working on particular URL

I have a redirect rule set up as below
Redirect /Products.aspx?Category_ID=15 https://www.trainerbubble.com/free-training-resources/
However, when going to the address it attaches itself to an existing page /training-products and results in this
https://www.trainerbubble.com/training-products/?Category_ID=15
How can I force the original redirect and make it stop thinking its part of the /training-products page?
You cannot match query string in Redirect directive. Use mod_rewrite rules instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Category_ID=15$ [NC]
RewriteRule ^Products\.aspx$ https://www.trainerbubble.com/free-training-resources/? [L,NC,R=301]
? at the end of target URL is to strip off previous query string.

Opencart 301 Redirects

Having a problem with redirect in a .htaccess file on an Opencart store.
It seems that any URL with /index.php?_route_= isn't getting redirected.
For example, this works:
redirect /old-url-here http://example.com/new-url?
This doesn't:
redirect /index.php?_route_=some-url.asp http://example.com
Any idea or suggestions as to what might get this to work?
You can't match against the query string using mod_alias' Redirect directive. You'll need to use mod_rewrite, and if you use mod_rewrite, you're probably going to want to stop using mod_alias altogether.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} route=some-url\.asp
RewriteRule ^index\.php$ http://example.com/
Another thing is - apart from Jon's answer - that URLs like index.php?_route_=some-keyword are used/created only internally and only in case you have the SEO turned on. In this case, you click on a link with URL like http://yourstore.com/some-keyword and this URL is rewritten into index.php?_route_=some-keyword.
Therefore you shouldn't be creating URLs like that on your own nor try to redirect from them. If you need to redirect, catch the first SEO URL to redirect it.
We do not know where did you put your changes into the .htaccess file, but if you take a close look at this rewrite rule
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
not only you'll find out it is the last rule there, but it also has this L switch which is telling Apache server that if this rule is matched, do a URL rewrite(, attach the query string [QSA] and) stop matching other rules [L] - this is the Last one.
If you need to redirect only a specific URL, do it the same way as redirect for sitemap.xml is done (for example) and place it before the last rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
# ...
# your new rule here - while the URL in the link is http://yourstore.com/some-url.asp
RewriteRule ^some-url.aspx$ index.php?route=some-folder/some-controller [L]
# ...
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

Apache Redirect If Contains String

I am trying:
RewriteRule ^\/product\/id\/7$ http://newdomain.com/whywonthisworkgrrr?id=1 [L,R=301]
And it won't work, I need to redirect all pages that have /product/id/7 anywhere in the URL.
need to redirect all pages that have /product/id/7 anywhere in the URL.
You can use this rule:
RewriteRule (^|/)product/id/7(/|$) http://newdomain.com/whywonthisworkgrrr?id=1 [L,NC,R=301]