How to redirect URL Friendly created with rewrite - apache

I created URL with rewrite mod.
This is the command:
RewriteRule ^productos/(.+)-(.+).html ecommerce/buscador.php?categoria=$2
Example the URL
http://www.frutasadomicilio.cl/productos/Verduras-2.html
The URL works good but now i need change the URL. I am thinking that with redirect 301 is my way an a solution. But when i try the redirect the URL, the command return the new url but with parameter.
The command is:
redirect 301 /productos/Verduras-2.html /productos/Verduras_a_domicilio-2.html
when I access the URL from google search, the Browser return this URL with parameter
http://www.frutasadomicilio.cl/productos/Verduras_a_domicilio-2.html?categoria=2
Any suggestion?
Thanks

Do not mix Redirect and RewriteRule for similar URLs. I suggest you to use only RewriteRule as
RewriteEngine on
RewriteRule ^productos/Verduras-2\.html$ /productos/Verduras_a_domicilio-2.html [R=301,L]
RewriteRule ^productos/(.+)-(.+)\.html$ ecommerce/buscador.php?categoria=$2 [NC,L]
Also, the order of the rules above is important.

Related

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

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.

Redirecting complex URL Aliases in htaccess

I need to redirect sample.com/newdomain/calendar to an external link however I already have a rewrite rule in place to redirect sample.com/newdomain/ to newdomain.sample.com/ so when I enter sample.com/newdomain/calendar into the web browser it takes me to newdomain.sample.com/calendar. How can I override the original rewrite I have written to allow sample.com/newdomain/calendar to redirect to an external url.
The rewrite rule that will redirect any url containing /newdomain is:
# Redirect /newdomain to newdomain.sample.com
RewriteRule ^newdomain(/.*)?$ http://newdomain.sample.com$1 [R=301,QSA,L]
Any help is appreciated!
You can place new redirect rule above the previous rule and make sure to clear your browser cache before testing:
RewriteEngine On
RewriteRule ^newdomain/calendar/?$ http://external.site.com/ [R=301,NC,L]
RewriteRule ^newdomain(/.*)?$ http://newdomain.sample.com$1 [R=301,NC,NE,L]

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]

How to rewrite /?a=a to /a/?

How can I rewrite this URL:
http://mywebsite.com/?a=a
To this URL:
http://mywebsite.com/a/
With htaccess?
I tried this rewrite rule but it's not working:
RewriteRule /a /?a=a
Try this instead :
RewriteRule ^a/?$ /?a=a [L]
This will route http://mywebsite.com/a/ requests to http://mywebsite.com/?a=a
If you are only looking for an exact match of ?a=a in the URI you can use this.
RewriteRule ^\?a=a$ /a/ [R=301]
This assumes you want to rewrite the actual URL in the user's browser and provide a 301 header to let anything crawling the page know about the "proper" URI to use (thus the [R=301]).

Properly 301 redirect an URL with parameters to a different site

Right now I have an outdated URL that is getting 404'd:
http://mysite.com/category/?tag=blue-example
It needs to be redirected to:
http://subdomain.mysite.com/blue/
This doesn't work:
Redirect 301 /category/?tag=blue-example http://subdomain.mysite.com/blue/
How do I properly rewrite this URL?
The mod_alias does only chech the URI path and not the query (the part after the ?). But you can use mod_rewrite for this:
RewriteEngine on
RewriteCond %{QUERY_STRING} =tag=blue-example
RewriteRule ^category/$ http://subdomain.example.com/blue/? [L,R=301]