Htaccess redirect if url contains a string? - .htaccess

I want to redirect url's who contains a string "cfg-contactform", the requested url contains sometimes mixed string like "cfg-contactform-15, 16, 22" etc... but I want to redirect all url's with string "cfg-contactform" including numbers or something others...
The solutions what I tried are:
https://webmasters.stackexchange.com/questions/24336/redirect-using-htaccess-if-a-url-contains-query-string
htaccess redirect if URL contains a certain string
redirect if url contains specific string using htaccess
htaccess redirect if URL contains a certain string
What I do wrong in my examples?
RewriteCond %{QUERY_STRING} cfg-contactform
RewriteRule .? / [R,L]
or
RewriteCond %{QUERY_STRING} cfg-contactform [OR]
RewriteCond %{REQUEST_URI} inc
RewriteRule ^ /? [L,R=301]

Related

Redirect from old url to new url patron

I want to add multiple redirect old website urls to new urls
Currently I have this url pattren
http://example.com/product/cat1/cat2/61600
I want to redirect all pages with the above pattern with the below one
http://example2.com/canbenaything-61600.html
However I want to redirect all urls on Last url part based on this case it is 61600.
For your particular case, add this in your .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^product/cat1/cat2/(.*)$ "http\:\/\/example2\.com\/canbenaything-$1.html" [R=301,L]
The following line will match the example.com which is a rewrite condition
RewriteCond %{HTTP_HOST} ^example\.com$
The following line will match the query string after product/cat1/cat2/(.*) and store what is there in place of (.*)
RewriteRule ^product/cat1/cat2/(.*)$
And the complete line will redirect to new URL
RewriteRule ^product/cat1/cat2/(.*)$ "http\:\/\/example2\.com\/canbenaything-$1.html" [R=301,L]
If you want to redirect http://example.com/anything/some_id to http://example2.com/something_else-some_id.html
then the following line would work
RewriteRule ^(.*)/(.*)$ "http\:\/\/example2\.com\/something_else-$2.html" [R=301,L]

Redirect entire domain with its corresponding URL and query string except one

I have a rule in my .htaccess that redirects an entire domain with its URL and query string to a new domain. However I want to exclude one URL from this. I want to still redirect that URL for strip out the query string from that. So for eg https://www.test1.com/search?type=content should be redirected to https://www.test.com/search stripping out the query string but for the rest of the URL the below is fine.
# Redirect users from test1.com to https://www.test.com
RewriteCond %{HTTP_HOST} ^(www.)test1\.com$ [NC]
RewriteRule ^ https://www.test.com%{REQUEST_URI} [R=301,L]
You can use this :
#redirect and discard querystring from /search?type=content
RewriteCond ℅{THE_REQUEST} /search/?\?type=content [NC]
RewriteRule ^ https://www.test.com%{REQUEST_URI}? [R=301,L]
# Redirect users from test1.com to https://www.test.com
RewriteCond %{HTTP_HOST} ^(www.)test1\.com$ [NC]
RewriteRule ^ https://www.test.com%{REQUEST_URI} [R=301,L]

htaccess redirect to different domain with query string containing arrays

I have a url like so:
subdomain.domain.com/level1/level2?product_ids[]=1&product_ids=2&product_names[]=Something&product_names[]=Something+Else
I want to redirect this to a new domain and preserve the query string so it should redirect to:
subdomain.domain2.com/level1/level2?product_ids[]=1&product_ids=2&product_names[]=Something&product_names[]=Something+Else
You can use this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1\.com$ [NC]
RewriteRule ^level1/level2/?$ http://subdomain2.com%{REQUEST_URI} [NE,L,R=301]

htaccess rewrite RegEx add static value between parameters

I'm trying to rewrite url's with htaccess. The request url length is variable.
It consists of the following:
http://[domain.com]/[countrycode]/[main-category]/[sub-category]/[sub-sub-category]/[product-name].html
Of course it could also have less categories.
Another option is only a product page, like:
http://[domain.com]/[countrycode]/[product-name].html
I want to rewrite these url's and add a static value between the [countrycode] and the rest of the url path. Also, I want to change .html to .aspx at the end of every rewritten URL.
I came as far as being able to rewrite a url with only the main category like:
http://example.com/en/main-category with this htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^(.*)example\.com [NC]
RewriteRule ^(.*)/(.*)$ https://example.com/$1/3/$2.aspx [R=301]
But if the url contains a trailing slash or more categories, the static value ("3") gets added in the wrong place.
to be clear, I want this structure:
request: http://example.com/en/main-category/sub-category/product-name.html
rewritten: https://example.com/en/3/main-category/sub-category/product-name.aspx
Thanks for your help!
You may use this rule in site root .htaccess:
RewriteEngine On
RewriteRule ^([a-z]{2})/(?!3/)(.+)\.html$ https://%{HTTP_HOST}/$1/3/$2.aspx [L,NC,NE,R=301]

htaccess redirects are "ignored"

Have looked at all the other posts regarding this, but can't seem to get it to work. There are a number of unrelated reasons why I can't change the Joomla config, items, etc., but in any event, it seems that these should work regardless of that.
In short, I want any link with Itemid=30 in it to redirect to the one given. What am I doing wrong? The first 3 are what I've tried, that last line is one that is working.
RedirectMatch 301 ^Itemid=30$ http://inside.beta.oursite.com/index.php?option=com_cware&view=courses&Itemid=125
redirect 301 index.php?option=com_cware&view=courses&Itemid=30 http://inside.beta.oursite.com/index.php?option=com_cware&view=courses&Itemid=125
RewriteEngine On
RewriteCond %{QUERY_STRING} Itemid=30
RewriteRule ^Itemid=30/$ index.php?option=com_content&view=article&id=106&Itemid=121 [R=301]
# If query string contains "username=admin"
RewriteCond %{QUERY_STRING} username=admin
# Block it without mercy
RewriteRule .* - [F]
Redirect considers only URL paths, which excludes the query string. If you want to take the query string into account, you must employ mod_rewrite.
Same applies to RewriteRule
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
Therefore, if you want to match some query string and redirect all URLs to some other URL, use a RewriteCond in combination with RewriteRule
RewriteEngine On
RewriteCond %{QUERY_STRING} Itemid=30
RewriteRule .* index.php?option=com_content&view=article&id=106&Itemid=121 [R=301]
This redirects any URL .* with the query string containing Itemid=30 to index.php?option=com_content&...

Resources