Cleansing special characters from URL search queries - regex

When I use the search form on my website, the URL ends up like this:
http://website.dev/catalogsearch/result/?q=WhatISearchedFor
I'd like to make it so that the URL is cleansed of special characters so that if I tried to go to
http://website.dev/catalogsearch/result/?q=$What#I&Searched\\For
I'd end up at the same URL above
Here is the current rewrite rule I have, but I'm new to regular expressions/htaccess so I'm not sure if it's formatted correctly.
RewriteRule ^catalogsearch/result/?q=(/[^a-z0-9\s\']/i)$
How would I go about doing this?

Related

Redirect Query String via .htaccess

I'm trying to redirect the user via the .htaccess file to create friendly URL example:
UI URL: https://example.com/courses/1
htaccess role
RewriteEngine On
RewriteRule ^courses/([0-9]{1})$ /courses.php?page=$1
Output URL: https://example.com/courses.php?page=1
And everything is working fine, Now I need to add other query params to the URL like this http://smart-courses.com/courses/1?p1=1&p2=2 so, I need htaccess to redirect me to https://example.com/courses.php?page=1&p1=1&p2=2
I tried to create a new role to check if p1 and p2 are exists and rewrite the URL
RewriteRule ^courses/([0-9]{1,5})?lid=([0-9]{1,})&did=([0-9]{1,})$ /courses.php?page=$1&p1=$2&p1=$3
Also, I tried to take any chars after ([0-9]{1,5}) (page number) and put it after ?page=1 but it did not worked
RewriteRule ^courses/([0-9]{1})\?(.*)$ /courses.php?page=$1&$2
The query string is not part of the path section of the URL that the rule pattern is matched against. If you'd have to capture something from the query string you need to use a RewritetCond, that is documented. In this case however you don't even need to capture anything:
RewriteEngine On
RewriteRule ^/?courses/(\d)$ /courses.php?page=$1 [QSA]
The QSA flag adds a potential query string to the (rewritten) target URL. The rewriting module takes care to use the correct concatenation here (the & character). Again, this behavior is documented: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Take a look yourself: htaccess.madewithlove.com

Redirect the URL from one query string to another

I have spent a great many hours trying to find a solution to this and tried many different approaches but nothing I have tried has worked so far.
I would like to redirect a URL with a query string to another URL that contains the value of that query string.
I want to redirect:
https://example.com/component/search/?searchword=XXXXXXXXX&searchwordsugg=&option=com_search
to
https://example.com/advanced-search?search=XXXXXXXXX
You can do something like the following using mod_rewrite at the top of your root .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)searchword=([^&]*)
RewriteRule ^component/search/?$ /advanced-search?search=%1 [NE,R=302,L]
The RewriteRule pattern matches against the URL-path only, which notably excludes the query string. To match against the query string we need a separate condition that checks against the QUERY_STRING server variable.
%1 is a backreference to the first capturing group in the preceding CondPattern, ie. the value of the searchworld URL parameter.
The regex (?:^|&)searchword=([^&]*) matches the searchworld URL parameter anywhere in the query string, not just at the start (as in your example). This also permits an empty value for the URL parameter.
The NE flag is required to prevent the captured URL parameter value being doubly encoded in the response. (Since the QUERY_STRING server variable is not %-decoded.)
The L flag prevents further processing during this pass of the rewrite engine.
Reference:
Apache docs: RewriteRule Directive
Apache docs: RewriteCond Directive

Apache Rule Rewrite Map Regular Expression

Hey i am trying to create a redirect map but the links i am trying to redirect are generated and just the ID number at the end is what matters.
I would like to know how can i redirect based on skipping everything in front of the last hyphen "-" in the url.
URL that i am trying to redirect:
http://staging.mysite.com/discover/en/suppliers/TD-Machi-LC-363868
but can be any possible combination:
http://staging.mysite.com/discover/en/suppliers/TD-Machining-101-LCC-363868
http://staging.mysite.com/discover/en/suppliers/Ted-Baker-D-363868
http://staging.mysite.com/discover/en/suppliers/363868
My current redirect that i was trying to get to work is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteMap discovertxt txt:/opt/bitnami/apps/wordpress/htdocs/maps/discover_all_map.txt
RewriteMap discoverdestinationtxt txt:/opt/bitnami/apps/wordpress/htdocs/maps/discover_destination_map.txt
RewriteMap discoversourcetxt txt:/opt/bitnami/apps/wordpress/htdocs/maps/discover_source_map.txt
# Discover Redirect
# RewriteCond ${discoverdestinationtxt:$1} >${discoversourcetxt:$1} [NC]
# Trying to capture only the last value of the string
RewriteRule ^/discover/en/suppliers/^(.*)-(.*)-(.*)-(.*) /manufacturer/${discovertxt:$4}/ [R=301,L]
# Bellow method works only if ID alone is supplied.
# RewriteRule ^/discover/en/suppliers/(.*) /manufacturer/${discovertxt:$1}/ [R=301,L]
</IfModule>
Answer for how to search for exact ID match within string:
RewriteRule ^/discover/en/suppliers/(.*)([0-9]{6}) /manufacturer/${discover:$2}/ [R=301]

Redirect all URLs starting with STRING that does NOT include a SUBSTRING

I need some help with regex.
I'm building some 301 rules for an .htaccess
I need to redirect all urls starting with a specific string excluding one that has a given word in the match-all part
this is the simple rule I'm using:
/my/sample/url/(.*)
I need to edit the (.*) part to say: anything except if contains "foobar"
if contains "foobar" I need a different 301 rule
This looks like is working:
^(?!.*foobar)/my/sample/url/(.*)
does anybody have a better solution?

Regex to Find URLs with only one slash

I am testing a website and need to target the pages I want to include in the test with Regex.
I will be targeting only product pages which all have a single slash in the URL (The URLs do not show http:// in them).
Here are the URLs I need to match:
The ones I want look like this:
www.example.com/just-one-slash
The ones I don't want look like this:
www.example.com/more-than-one/slash
www.example.com
This should work for your case: ^[^/]+/?[^/]+$
For the answer to be generic, with a trailing slash it would just need /? at the end, like this: ^[^/]+/?[^/]+/?$