Htaccess - Remove end of URL when it contains a word - .htaccess

I want to remove the ?search query and the end of the URL, provided the URL contains the word "design".
For example, if the htaccess file contained this rule, it will transform:
https://www.example.com/designs/brandicons/Dinodento/?search=dino
to
https://www.example.com/designs/brandicons/Dinodento/
but would not touch:
https://www.example.com/product/search/?search=dino
..as it doesn't contain the word "design".
Any help would be greatly appreciated :)

You can use this rule in site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^designs/.*$ /$0? [L,R=301,NC]
It will match and URI starting with /designs/ that has ?search= query string. In replacement /$0? will strip off query string.

Related

Drupal 7 - htaccess redirect if URL contains a certain string

How would I write a redirect rule in my htaccess file if the URL contains a certain string?
Example:
https://www.mywebsite.com/_Incapsula_Resource?SWJIYLWA=2977d8d74f63d7f8fedbea018b7a1d05&ns=2&cb=1143295674
https://www.mywebsite.com/_Incapsula_Resource?SWJIYLWA=2977d8d74f63d7f8fedbea018b7a1d05&ns=3&cb=1461264764
https://www.mywebsite.com/_Incapsula_Resource?SWJIYLWA=2977d8d74f63d7f8fedbea018b7a1d05&ns=1&cb=1693249588
If it contains _Incapsula_Resource then redirect to https://www.mywebsite.com
Any help will be greatly appreciated.
Got it.
Instead of _Incapsula_Resource, use SWJIYLWA=
RewriteCond %{QUERY_STRING} SWJIYLWA=
RewriteRule .* /? [R,L]

Remove word from URL and redirect to resultant URL

I currently have a product URL withe the format
https://www.maydomain.com/proddetail.php?prod=ACME-XYZ-Golden-Widget-001
and would like to remove the word 'Golden' from all URLs such that they become
https://www.maydomain.com/proddetail.php?prod=ACME-XYZ-Widget-001
AND the old URL now redirect to the new URL.
Any suggestions anyone?
If the replacement is restricted to query string alone, try the following:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(prod=.*)-golden-(.*)$ [NC]
RewriteRule ^proddetail\.php$ /$0?%1-%2 [R=301,L]

htaccess rewrite - removing characters from url

Looking to rewrite a part of a URL and stuck with dealing with special characters.
original
http://www.testwebsite.com/Products/Apple/*/!Accessories
desired result
http://www.testwebsite.com/Products/Apple-Accessories
I would also like to redirect (can a word be removed?) testwebsite.com/Products/Dell-Laptop/*/!Accessories
to testwebsite.com/Products/Dell-Accessories
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^(Products/[^/]+)/\*/!(.+)$ /$1-$2 [R=301,L,NE]

restricting input of GET url on htaccess rewrite url

My original url is : www.site.com/report.cgi?d=2012-05
Requested URL: www.site.com/report-2012-05.cgi
My Htaccess Code:*
RewriteRule ^report([^/]*)\.cgi$ /report.php?d=$1 [L]
I want to restrict the request parameter to just XXXX-XX number format in GET url.
How can I do this ?
I didn't really understand your question, except you want to modify the URL format placing the parameter value in a different position.
The best way to do it is by capturing the query string like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} d=(.*)
The value inside the round brackets is the parameter value (2012-05), which can be back referenced with %1. For example:
RewriteRule .* report-%1.cgi [L]
Will rewrite the URL with /report-2012-05.cgi
Hope this helps.
I think you need to remove .cgi from your rewrite rule
For www.site.com/report-xxxx-xx
RewriteRule ^report-([^/]*)$ /report.cgi?d=$1 [L]<br>
For www.site.com/xxxx-xx
RewriteEngine On
RewriteRule ^([^/]*)$ /report.cgi?d=$1 [L]

.htaccess 301 redirection problem

I am having trouble writing a rule for following problem in .htaccess. Here is what I need.
WHEN URL IS THIS
www.mysite.com/cat/2011/subcat1/subcat2/product.htm?page=2
IT SHOULD 301 REDIRECT TO THIS
www.mysite.com/cat/2011/subcat1/subcat2/product/2.htm
Can you please tell me how to do this?
Thanks
----- EDITED AND TESTED ANSWER ----------
RewriteEngine On
RewriteCond %{QUERY_STRING} page=(\d+)
RewriteRule ^cat/(\d+)/([^/]+)/([^/]+)/product\.htm http://www.mysite.com/cat/$1/$2/$3/product/%1.htm? [R=301,L]
RewriteRule matched only the url part, not the query string. So, RewriteCond is used to match query string.
Please note, matches from the url are used as $N and match from the query string is used as %N
There is a question mark placed in the end of the rewritten url. This prevents the original query string to be added to the new rewritten url.
For any number of subcats:
RewriteEngine On
RewriteCond %{QUERY_STRING} page=(\d+)
RewriteRule ^cat/(\d+)/(.+)/product\.htm http://www.mysite.com/cat/$1/$2/product/%1.htm? [R=301,L]

Resources