Forum Moderators: phranque

Message Too Old, No Replies

How to rewrite a search string in htaccess

How to rewrite a search string for seo friendly URLs in htaccess

         

jonoweb

3:36 pm on Nov 30, 2020 (gmt 0)

5+ Year Member



My first post, Hello!

I need help on this rewrite task I have to do. I have purposely avoided htaccess and regular expressions in the past because it just looks so complicated, keen to learn though.

Here's what I want to do:

Query String:

/search.php?q=manager&city=london?page=1&distance=0


Convert to:

/{q}-jobs/{city}.html?page=1&distance=0


Example:

/manager-jobs/london.html?page=1&distance=0


If {city} empty rewrite like so:

/{q}-jobs/national.html


If {q} empty rewrite like so:

/jobs/{city}.html?page=1&distance=0


If {q} and {city} are empty rewrite URL like so:

/jobs/national.html?page=1&distance=0

tangor

12:50 am on Dec 1, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@jonoweb ... Welcome to Webmasterworld!

My rewrite skills are pretty mediocre, but do not despair, there's a boat load of experts here to point the way!

dstiles

2:16 pm on Dec 2, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have recently modified a site from ASP to PHP and have opted to invoke searches using POST buttons instead of querystrings I previously used. Reason for this: I block almost all querystrings as potential hacking and injecting mechanisms. POST is much easier to control. I feed all searches into a single page but pass along the POST parameters.

phranque

3:26 am on Dec 6, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], jonoweb!

we'd love to help, but first you should read the "Fix my code" and "Do my homework for me" threads section of the Apache Web Server forum Charter [webmasterworld.com].

Here's what I want to do:

what you really want to do entails 2 stages of this process that must be implemented:
- you need to redirect external requests for something like /search.php?q=manager&city=london?page=1&distance=0 to something like/manager-jobs/london.html?page=1&distance=0 using a 301 status code.
- you need to rewrite external requests for something like/manager-jobs/london.html?page=1&distance=0 to an internal request for something like /search.php?q=manager&city=london?page=1&distance=0

you can do this all using the mod_rewrite [httpd.apache.org] apache module.

this thread describes this process and an example:
SEO friendly URLs [webmasterworld.com]

let us know what you have tried and what results you got.

jonoweb

12:58 pm on Dec 7, 2020 (gmt 0)

5+ Year Member



Thanks for all the tips and advice.

It was a bit of a lazy post tbh, I've since found a few good tut's on regexp, not as complicated as I thought, once I crack that I should be away.

Hi @dstiles - Why would you say POST is easier to control as appose to GET? I know POST keeps data hidden but I prefer GET for some reason.

phranque

1:17 am on Dec 8, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



..., not as complicated as I thought, once I crack that I should be away.

be sure to ask questions here or start a new thread if appropriate.

dstiles

9:59 am on Dec 8, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why would you say POST is easier to control as appose to GET?

In htaccess or equivalent, I can say, "Kill all querystrings" without having to determine which parameters are acceptable - a lot of injection stuff arrives in querystrings. For Post I simply trap everything except a known set of pages, which is very small and consistent, such as "contact" and "feedback".

jonoweb

10:36 pm on Dec 11, 2020 (gmt 0)

5+ Year Member



"In htaccess or equivalent, I can say, "Kill all querystrings" without having to determine which parameters are acceptable"

Not sure I understand exactly can you give an example?

Right so I think I understand the rewrite / redirect process as in how it works in relation to front end - back end processes and back again:

Pretty URL (front end) > URL rewrite pretty > Server > Old URL > Process code > rewrite / redirect URL > Pretty URL (front end)

Correct?

dstiles

1:37 pm on Jan 12, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



> Kill all querystrings

If query_string is not empty, stop the connection. There are a small number of acceptable query_string values but they are easily excepted.

lucy24

6:22 pm on Jan 12, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Pretty URL (front end) > URL rewrite pretty > Server > Old URL > Process code > rewrite / redirect URL > Pretty URL (front end)
Ah, the redirect-rewrite two-step, required when you've changed your mind about which part goes in the query string. (But moving some parts to the main URLpath while keeping other parts in the query seems awkward. Make sure this is really what you want to do.)

You will end up with two things. In the part of your htaccess that deals with external redirects (“external” doesn’t necessarily mean go to another site, it just means you’re sending a message back to the requesting visitor):
RewriteCond %{QUERY_STRING} blahblah
RewriteRule ^old-messy-url https://www.example.com/new-pretty-url [R=301,L]
(possibly replacing QUERY_STRING with THE_REQUEST depending on, well, stuff) and then, later, in the section dealing with internal rewrites,
RewriteRule ^pretty-url /messy-url-with-query [L]

The fact that you’re keeping part of the query and throwing out other parts is a complication. You will need to capture each parameter, putting some parts into the URLpath and other parts into the new shorter query. If you’re not careful, you will end up either losing parameters you want to keep, or duplicating ones you meant to get rid of. And, because of the way mod_rewrite handles Conditions, you have to do all the capturing from a single RewriteCond; you can't accumulate them line by line.

It will definitely be less painful if you can be certain that your parameters--in the older, messier URLs--always came in the same order. Otherwise you may have to set up multiple rules to cover all possible permutations.

jonoweb

9:05 pm on Jan 23, 2021 (gmt 0)

5+ Year Member



Cracked it! :D To say I'm ecstatic is an understatement.

Thank you so much for the info and advice, especially Lucy for answering with such detail, I am very grateful.

It was also helpful to learn that htaccess gets processed before any server side PHP, learning this helped a great deal to put together in my mind how everything is processed.

Will follow up tomorrow and share details of how/what I did to achieve the results.

:)