I searched on the site, but i didn´t find an exact answer.
I have multiple urls in a wordpress site with different parameters like this:
http://www.example.com/folder/?filter_color=353&orderby=price-desc
http://www.example.com/folder/?filter_material=345&orderby=date
http://www.example.com/folder/?filter_size=43&price-asc
I need to permanent redirect all of them to http://www.example.com/folder/
Is it possible with htaccess? Thanks!
EDIT 1:
I tried with this code but it doesn´t work. I want to redirect those old urls because they lead to a nothing found page. So after i copy this code and save the htaccess, nothing changes. The nothing found page still appear and the old url is shown in the browser. So it does nothing.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder$
RewriteCond %{QUERY_STRING} ^filter_material=([0-9]*)$
RewriteRule ^(.*)$ http://www.example.com/folder/ [R=301,L]
EDIT 2:
I finally achieved what i wanted with this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^filter_([\w]*) [NC]
RewriteRule ^folder/$ http://www.example.com/folder/? [R=301,NE,NC,L]
The rewrite condition for the query string is a regular expression, which is a tool for matching against search patterns.
The regular expression you are using – ^filter_material=([0-9]*)$ would match against URLs with a query string that starts with filter_material=, followed by an arbitrary number of digits.
The URL you want to redirect, however, does not match here, because it also contains &orderby=date, which is not all digits.
Since all the URLs you want to redirect start with filter_, you can simply use a regular expression that matches any string that starts with that, like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder/?$
RewriteCond %{QUERY_STRING} ^filter_.+$
RewriteRule ^(.*)$ http://www.example.com/folder/ [R=301,L]
The period means “any character”, the plus means “none, one, or more than one”.
Note that I've also added /? to the rewrite condition for the request URI, which means “there could or could not be a slash after folder”.
You can test this rewrite rule here: http://htaccess.mwl.be?share=2329d6f6-4fd5-5ff2-9035-a1957a242a42
I finally achieve what i wanted with this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^filter_([\w]*) [NC]
RewriteRule ^folder/$ http://www.example.com/folder/? [R=301,NE,NC,L]
Related
I would like to redirect the following URL
https://www.example.com/vmi10/? to https://www.example.com/vmi10/
This is a WordPress site. How can I do that? I really appreciate any help you can provide.
You may use this redirect rule as your topmost rule in main .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+[^?]*\?\s
RewriteRule ^ %{REQUEST_URI}? [L,NE,R=301]
# other WP rules below this line
Basically you want to remove empty query string from the url we use ^$ in our RewriteCond Condition Pattern like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) /$1? [R=301,L]
This will redirect https://www.example.com/vmi10/? to https://www.example.com/vmi10/
This won't affect any url with non empty query string.
Test it here: https://htaccess.madewithlove.be?share=34570386-6b48-486b-ba84-32a222921502
Please help me to write the correct RewriteRule for the 301 redirect (.htaccess under subdomain blog.example.com) for the links like:
from
https://blog.example.com/?source=tag_archive---------0-----------
redirect to
https://example.com/blog/
I use these lines in .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^source=tag_archive---------0-----------(&.*)?$ [NC]
RewriteRule ^(.*)$ https://example.com/blog/ [R=301,NE,NC,L]
With the example above I got
https://example.com/blog/?source=tag_archive---------0-----------
but need
https://example.com/blog/
(without part ?source=tag_archive---------0-----------)
On Apache 2.4+ you need to add the QSD (Query String Discard) flag to the RewriteRule directive:
RewriteRule ^ https://domainname.com/blog/ [QSD,R=301,L]
(Alternatively, if you are still on Apache 2.2, then append a ? to the end of the substitution string - effectively an empty query string. The ? itself will not be in the redirect response.)
^(.*)$ (the RewriteRule pattern) - since you are matching any URL-path and not using the backreference, this can be simplified to ^.
The NC and NE flags are not required here.
However, as noted above, this redirects any URL-path. If you specifically want to redirect https://blog.example.com/?source=tag... only then change the RewriteRule pattern to ^$ (an empty URL-path). For example:
RewriteRule ^$ https://domainname.com/blog/ [QSD,R=301,L]
So I'm using the answer from here to remove the query string: How to remove GET parameters from url with htaccess?
And it works fine in general.
Here are the rules I'm using (in the beginning of the htaccess):
RewriteEngine on
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} !^p=
RewriteRule (.*) https://www.r-bloggers.com%{REQUEST_URI}? [R=301,L]
BUT...
If I browse using a mobile device to this url:
t.co/6raI2VkTse?amp=1
It will redirect me to this url (and will stay there, without a further redirect, even though it has a url parameter):
https://www.r-bloggers.com/how-to-handle-cran-checks-with-help-from-r-hub/amp/?p=182189&__twitter_impression=true
If I then refresh, it will redirect me to:
https://www.r-bloggers.com/how-to-handle-cran-checks-with-help-from-r-hub/
I've tried adding:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)&?__twitter_impression=[^&]+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /$1?%1%2 [R=301,L]
(from here)
But this also didn't help.
I have no idea why this is happening. Any suggestions?!
I wanna redirect all my wordpress pages from one domain to another, such as:
from: http://domain1.com/page/
to: domain2.com/page/
So I added to htaccess of domain1.com:
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]
But I want to make certain files exceptions to the above rule, so I also added:
RewriteCond %{REQUEST_URI} ^(images|javascripts|stylesheets|css|images-globa|js-global|js|htm|html).*
but the second rule doesn't seem to work. :(
Those aren't exceptions, those are "the request MUST be images/javascripts/stylesheets/etc in order to redirect", so you've got it backwards. You want a ! in there:
RewriteCond %{REQUEST_URI} !(images|javascripts|stylesheets|css|images-globa|js-global|js|htm|html)
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]
Though I'm assuming you're way overreaching with the regular expression there, some of those look like you are matching against only the extension, so:
RewriteCond %{REQUEST_URI} !(images|javascripts|stylesheets|css|images-globa|js-global)
RewriteCond %{REQUEST_URI} !\.(js|html?|png|je?pg|gif)$ [NC]
RewriteRule (.*)$ http://www.domain2.com\/$1 [R=301,L]
Depending on my value IS_HTTP, I decide after the code below, whether URL should be https or http.
I want always http (redirected from https) for http://mydomain.com (has an empty QUERY_STRING), http://mydomain.com/?q=home, http://mydomain.com/?qq=home.
And the code below works fine for such URLs, it sets up IS_HTTP to 1. Everything is perfect. But I want also URLs to administration folder always to be httpS, so I want to exclude such URLs from that block.
That's why I added the second string to the code below, but it doesn't stop such URLs from applying RewriteRule ^ - [E=IS_HTTP:1]. Why?
#determine if page is supposed to be http
RewriteCond %{REQUEST_URI} !^administration [NC]
#if it has q=home or qq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(q=home|qq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
Again, I want https://mydomain.com/administration/index.php and all other files from the folder /administration to be stopped before RewriteRule ^ - [E=IS_HTTP:1]
Why the code above does NOT stop them?
The second string I've tried was:
RewriteCond %{REQUEST_URI} !^administration [NC]
RewriteCond %{REQUEST_URI} !^/administration [NC]
but none of them works. (I believe / is not needed here as REQUEST_URI doesn't start from /, but I could be wrong).
Thank you.
Instead of this condition:
RewriteCond %{REQUEST_URI} !^administration [NC]
have this condition:
RewriteCond %{REQUEST_URI} !^/+administration [NC]
Remember that %{REQUEST_URI} always starts with a slash and can have multiple slashes if it is typed that way in browser.