Rewrite URL empty values in query string - Double slashes, Optional Parameters - .htaccess

I have looked around quite a bit and tried myself and could not sort it out.
This is the URL before applying rewrite :
http://example.com/job-search/?searchText=GOOGLE+INC.&searchCity=Enter+US+City+or+Zipcode&searchYear=14&action=search&searchJobTitle=Enter+Job+Title+%2F+Role+Name
This is the URL after below rewrite rules are applied :
http://example.com/job-search/GOOGLE-INC./Enter-US-City-or-Zipcode/14/search/Enter-Job-Title
I have the below rules in .htaccess. It works well, when I have the URL like above with all the fields.
RewriteEngine On
RewriteBase /job-search/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R]
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=([^\s&]+)&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)/(\d+)/([^/]+)/([^/]+)/?$ ?searchText=$1&searchCity=$2&searchYear=$3&action=$4&searchJobTitle=$5 [NC,L,QSA]
The rewrite fails, when It gets empty values. The rules do not work and I get the below URL.
http://example.com/job-search/?searchText=GOOGLE-INC.&searchCity=&searchYear=14&action=search&searchJobTitle=
Of the three parameters ( searchText or searchCity or SearchJobTitle ), at a time, only one of the parameters will be having a value and other two will have blank values. Also, just want to ensure, when the user types the home page URL like http://example.com/job-search/ , it should not fail ( I tried something and it failed ) . Would like to avoid double slashes as well, when they are empty.
Below is the output I would like to see :
E.g when searchCity and SearchJobTitle are empty, the output URL should look like
http://example.com/job-search/GOOGLE-INC./14/search
When searchText and searchCity are empty and searchJobTitle value is 'Applicaton-Engineer', the output URL should look like
http://example.com/job-search/14/search/Application-Engineer
Can you please help me get the right rule ? Appreciate your help !

Change your rules to this:
RewriteEngine On
RewriteBase /job-search/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R]
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=\s [NC]
RewriteRule ^ %1/%2/%3/? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=([^\s&]+)&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)/(\d+)/([^/]+)/([^/]+)/?$ ?searchText=$1&searchCity=$2&searchYear=$3&action=$4&searchJobTitle=$5 [NC,L,QSA]
RewriteRule ^([^/]+)/(\d+)/([^/]+)/?$ ?searchText=$1&searchYear=$2&action=$3 [NC,L,QSA]

Related

How do I ignore the end of a url parameter string in Htaccess?

I have a bunch of ugly links coming in to a site like this:
https://www.somedomain.com/mm5/mch.mvc?Session_ID=5f59c6e0&Screen=PRODFB&Product_Code=pcode1&Category_Code=category_a&Store_Code=store1&fb=1
I'm trying to use htaccess to recreate the url like this:
https://www.somedomain.com/mm5/mch.mvc?Screen=PROD&Product_Code=pcode1
I've come up with this but of course it isn't working. I think I just need to be able to ignore the rest of the url parameter after the product_code param but not sure how to do it.
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^Screen=PRODFB&Product_Code=([^/.]+)$ /mm5/mch.mvc?Screen=PROD&Product_Code=$1 [R=301,L,NE]
What am I missing? Thanks!
You cannot match query string in RewriteRule directive.
As long as query parameters are same as what you have in question, you may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /(mm5/mch\.mvc)\?.*&Screen=PRODFB&(Product_Code=[^\s&]+) [NC]
RewriteRule ^ /%1?Screen=PROD&%2 [R=301,L,NE]
Or using %{QUERY_STRING}:
RewriteCond %{QUERY_STRING} (?:^|&)Screen=PRODFB&(Product_Code=[^&]+) [NC]
RewriteRule ^mm5/mch\.mvc/?$ %{REQUEST_URI}?Screen=PROD&%1 [R=301,L,NE]
I was able to get it work like this:
RewriteCond %{QUERY_STRING} Screen=PRODFB&Product_Code=([^&]+)
RewriteRule ^(.*)$ /mm5/mch.mvc?Screen=PROD&Product_Code=%1& [R=301,L,NE]

Remove parenthesis from htaccess (Helicon Ape)

I'm using Helicon Ape on a Windows Server to create htaccess files.
Originally, part of a larger set of conditions, I had this condition set to return 403 if the url contained (). However it is causing false positives in case of mailchimp tracking codes that end up getting wrapped in ()
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR]
For example in the below URL
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
As an alternative, I was attempting to remove the parenthesis and redirect to a "cleaned version".
I tried a few different things that I found in SO but none worked.
So far the closest thing that I could get to working is this:
RewriteCond %{REQUEST_URI} [\(\)]+ [OR]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
The problem with the above code is that works if the () were in the URL but not the query string. It doesn't redirect and clean the querystring.
So this would work:
http://domain.com/page/11/pag(e-name)
but this wouldn't:
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
Your assistance is appreciated
Thank You.
You can use the following rule :
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=t\(Newsletter_Tracking\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
If the querystring is dynamic, try:
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=.+\(.+\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
Using #starkeen 's example, I was able to create a working solution.
This code handles the Query String separate from the URL. It cleans the URL but removes the query string.
RewriteCond %{REQUEST_URI} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

.htaccess Rediret dynamic url to custom url

I have a URL structure like this
localhost/sumar/gallery.php?gen=slug-here
that i want to clean up for example: localhost/sumar/gallery/slug-here
so i need rewrite rule for this condition and prevent anyone from directly hitting the base URL http://localhost/sumar/gallery.php?gen=slug-here by auto redirecting it to localhost/sumar/gallery/slug-here
So far i have tried this method:
//that works
RewriteRule ^Gallery/([A-Za-z0-9-_]+)/?$ gallery.php?gen=$1
//below one does not seems to work
RewriteCond %{THE_REQUEST} ^GET\ /sumar/gallery\.php\?gen=([\w-]+) [NC]
RewriteRule ^sumar/gallery\.php$ /sumar/Gallery/%1? [R=301,L]
You'd need to act on the raw requests:
RewriteEngine On
RewriteRule ^(sumar)/Gallery/([\w-]+)/?$ /$1/gallery.php?gen=$1
RewriteCond %{THE_REQUEST} ^GET\ /sumar/gallery\.php\?gen=([\w-]+) [NC]
RewriteRule ^sumar/gallery\.php$ /sumar/Gallery/%1? [R=301,L]
PS: The character set [A-Za-z0-9_] can be represented with \w.

Cut off parameter from redirect in htaccess

This silent redirect in htaccess:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]
redirect a url that looks like:
example.com/album_showpage.php?pic_id=1906
to:
example.com/gallery/image_page.php?image_id=1906
that works great. But when the URL has a parameter like:
album_showpage.php?pic_id=1906&mode=prev
or
album_showpage.php?pic_id=1906&mode=next
the redirect wont work.
Question: How to cut of any parameter after pic_id=1906
thank you
You need match against the rest of the query string.
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)(&.*)?$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1%2 [L]
If you don't want the mode=prev stuff to be included in the rule's target, then you can simply remove the $ instead of attempting to match against it:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+) [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]

Redirecting %{QUERY_STRING} to friendly url

I'm trying to add one last improvement (regarding htaccess), and that is that I would like it to redirect /?mod=inicio to /inicio. So I'm trying to do it with the following code, but It keeps building the url like this /inicio?mod=inicio
RewriteCond %{QUERY_STRING} ^mod=([a-z]+)$ [NC]
RewriteRule .* /%1 [L]
Same thing with extra parameters: from /?mod=x&type=y-z to /x/y-z
Try these rules instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?mod=([a-z]+)&type=([a-z\-]+)
RewriteRule ^ /%1/%2? [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?mod=([a-z]+)($|\ )
RewriteRule ^ /%1? [L]
The key is to match against the actual request instead of the URI because your other rules (from your previous question) will cause these rewrites to match, they'll conflict with each other. The other key point is to include a ? at the end of the targets (e.g. /%1/%2?) which makes it so the query string doesn't get appended.

Resources