Dynamic htaccess RewriteRule [duplicate] - regex

I want to redirect links containing characters like %C2%A0 on my htaccess file
example:
redirect 301 /actualite/presses-plieuses-electriques%C2%A0-les-5-criteres-de-choix_343.html https://www.mynewsite.com
is not working
redirect 301 /actualite/presses-plieuses-electriques-les-5-criteres-de-choix_343.html https://mynewsites.com
is working
How do I replace %C2%A0 and %E2%80%99 characters on mylinks ?

You can use this RewriteRule rule:
RewriteEngine On
RewriteRule ^actualite/presses-plieuses-electriques\xC2\xA0-les-5-criteres-de-choix_343\.html$ / [L,NC,R=301]
Note use of \xC2\xA0 for %C2%A0

Related

Redirecting using regular expression in .htacces

I have to redirect urls like
www.site.com/?page=user?id=5
to something like
www.site.com/users/5
I wrote this regular expression
.*\?page=(.*)&id=(.*)
But it doesn't seem to work?
Full redirect rule:
Redirect 302 ^/.*index.php\?page=(.*)&id=(.*) /$1/$2
Redirect directive works on URL path only ie /index.php the part after ? is part of QUERY_STRING and it can not be matched using Redirect, You need to use mod-rewrite
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=([^&]+)&id=(.+)$ [NC]
RewriteRule ^index\.php$ /%1/%2? [L,R]

htaccess - can't for the life of me match this URL

http://www.matthewsvolvosite.com/for...p?f=11&t=17479
Need to match and 301 redirect that. I've tried
RedirectMatch 301 ^/for\.\.\.p?f=11&t=17479$ /forums/viewtopic.php?t=17479
and
RedirectMatch 301 ^/for(.*)f=11&t=17479 /forums/viewtopic.php?f=2&t=17479
Neither work. I've searched and tried many examples. I'm at a loss.
The .htaccess file does match and redirect many other strings.
RewriteBase /
is active and working.
You need to use mod-rewrite
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=11&t=17479$
RewriteRule ^for\.\.\.p$ /forums/viewtopic.php?t=17479 [NC,L,R]
The reason why your redirect is not working is because you are using mod-alias to handle query strings .

Wildcard 301 redirect using regular expression

Any help with this would be great as I am struggling to get this to work.
I am trying to do a wildcard RedirectMatch 301 redirect for /pop.php?path=content/ using
RedirectMatch 301 /pop.php?path=content/.* http://mywebsite.com
But due to the ".", "?" and "=" is it not working correctly and I am not good with regular expressions.
Thanks
Steve
Put this in a .htaccess file in your site root:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} path=content [NC]
RewriteRule pop.php http://mywebsite.com/? [R=301,L]
You need the RewriteCond to inspect the value of the query string. If and only if it's a match, pop.php will be redirected to the other site.

? appending to end of pathname after redirect 301 in htaccess file

This should be simple but I've not been able to work out the problem. I'm sure it's a seconds answer for somebody.
I have a rewrite in my htaccess file as follows:
RewriteCond %{THE_REQUEST} \ /product\.php\?categoryfilename=([A-Za-z0-9-%.]+)&category=([A-Za-z0-9-%.]+)&id=([A-Za-z0-9-]+)
RewriteRule ^ /%3-P.html? [R=301,L]
The rewrite works
If I enter:
www.mysite.co.uk/product.php?categoryfilename=all-products.php&category=All%20products&id=MY-PRODUCT
it redirects to:
http://www.mysite.co.uk/MY-PRODUCT-P.html
THEN I have what should be a simple redirect:
Redirect 301 /MY-PRODUCT-P.html http://www.mysite.co.uk/MY-PRODUCT-123-P.html?
This should be a static redirect but it is appending a pesky ? to the end of the result.
If I remove the ? from the end of the redirect then I get php variables appended left over from the original messy pathname in the first rewriterule
Many thanks
You can't match or manipulate QUERY_STRING using Redirect directive. Use mod_rewrite instead. Place this code in your DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/product\.php\?categoryfilename=([^&]*)&category=([^&]*)&id=([^&\s]*)
RewriteRule ^ /%3-P.html? [R=301,L,NE]
RewriteRule ^(MY-PRODUCT)-(P\.html)$ /$1-123-$2? [L,NC,R=301]

Redirect that starts with a?

I need to have a redirect done to a site I have that is a 404 error.
Redirects are done in a .htaccess file for Joomla. This is what I have currently:
redirect 301 /?view=category&id=12:bestfriend&Itemid=946&start=20 http://www.mysite.com
But it doesn't redirect because it starts with a "?".
Any ideas? I have a number amount of these like this - not just one.
You cannot match query string using Redirect directive. Use mod_rewrite like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^view=category&id=12:bestfriend&Itemid=946&start=20 [NC]
RewriteRule ^ http://www.mysite.com/? [R=301,L]