Apache Redirect If Contains String - apache

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]

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

apache redirect to remove a subfolder from the url

I have issues redirecting a specific url within the same domain
Example, I want to redirect
http://example.tld/folder/*
to
http://example.tld/*
where * is a wildcard
Basically you will need this type of code in your .htaccess file:
RewriteEngine On
RewriteRule ^/folder/ http://domain.tld/? [R=301,L]
This should redirect your current url http://domain.tld/folder to http:/domain.tld/
Or:
RewriteEngine On
RewriteRule ^folder/(.*)$ /$1 [R=301,NC,L]
Please try and see which one works best for you

htaccess redirect double slash URL 443

I have a webpage that's reachable via HTTPS and I have some Urls with a double-slash (//) included. Those I want to replace with single-slash (/) or just rewrite/redirect to another page.
https://www.domain.com/us//whatever/page/
https://www.domain.com/us/whatever/page/
Normally I do redirects like this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^us/xyz/?$ https://www.domain.com/us/newpage/? [NC,L,R=301]
And double-slashes (//) in the RewriteRule don't work for for the htaccess redirects :/
I've already searched for a solution for replacing // with / and I found:
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
But when I use this snippet and I open the URL with the double-slash - this happens:
Opened url: https://www.domain.com/us//whatever/page/
Redirects to: http://www.domain.com:443/us/whatever/page/
Need help here. How can I fix this?
And is there an additional way to rewrite just a specific url that includes a double-slash (//)?
Thank you very much!

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.

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]