0

My site uses a URL structure like below

example.com/CATEGORY/CITYNAME/TITLE/ID/

However sometimes the content might not have the category defined so would just be

example.com/CITY/TITLE/ID/

In my .htaccess I have

RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ page.php?jobid=$3
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)?$ page.php?jobid=$4

This works fine with or without the category I have an autocomplete textinput on the category page which adds the cityname to the URL In the .htaccess I have

RewriteRule ^warehouse-jobs$ catpage.php?id=10&kw=Warehouse&base=warehouse-jobs [L,NC]
RewriteRule ^.+/([^/]*)/$ catpage.php?id=10&kw=Warehouse&base=warehouse-jobs&loc=$1 [L]

So this will redirect the category without the city defined to the catpage ( which works ) But as soon as the city is added (2nd redirect rule ) it redirects to the domain root for some reason

I know I'm doing something wrong but I can't figure out what or how to remedy it Any advice would be very much appreciated

  • Although you didn't ask for it, have you looked into handling this in [pure PHP](https://stackoverflow.com/a/16389034/231316)? There's a single-line `FallbackResource` directive for htaccess that routes everything not found to a single PHP script, and from there you can (at least in my opinion) debug and test much easier. – Chris Haas Jun 30 '23 at 12:52
  • I havent but will certainly have a look at this now although just having a quick glance I can still see myself getting confused with this as well – Complete Noob Jun 30 '23 at 12:55
  • I would recommend that you read up on the [front controller pattern](https://en.wikipedia.org/wiki/Front_controller) (it's similar to what @ChrisHaas suggested above, but you can do it for all routes) and then use a [router](https://packagist.org/?query=router) to handle the URL's (which you set up in your application) instead of adding a bunch of different rules in your htaccess. It's how most frameworks and CMS's do it. And since you only need one single rule (that redirects all requests to non-existing files to index.php), it's easier to move it between web servers (not all use htaccess). – M. Eriksson Jun 30 '23 at 13:04
  • Having a look now although regarding the categories I have 42 category names passing a unique ID so I would still need to define each of them For example the CATEGORY in my example would be replaced with warehouse-jobs, accounting-jobs etc etc. I did have it working but then tested the page again today despite not changing anything at all and now its acting like this – Complete Noob Jun 30 '23 at 13:08
  • Here's a very simple version of your first two rules: https://3v4l.org/Gmj0E The server it is on doesn't support certain things so I had to stub them in, but hopefully you get the gist. What's arguably most important if that you can dump variables out at any given point and inspect what the heck is going on. – Chris Haas Jun 30 '23 at 13:11
  • You don't necessarily need to support 42 category names, you can still totally use RegEx inside of PHP if you want/need to, but the supplied patterns `([^/\.]+)` are so generic, just "at least one thing that not a forward slash or period" that I don't know if adding the RegEx into the mix would add any specific value. – Chris Haas Jun 30 '23 at 13:14
  • Thanks both of you. Can see how that works. I'm going to have a play around and see if I can get it working using the suggestions above and will update shortly – Complete Noob Jun 30 '23 at 13:17
  • To your problem at hand, I honestly moved away from writing htaccess rules a while ago, and my server doesn't even support them (although it does support an equivalent thing), so I'm pretty rusty. But shouldn't your last rule be more like `^warehouse-jobs/([^/]*)/$`? – Chris Haas Jun 30 '23 at 13:25
  • Regarding the warehouse-jobs rule it may or may not contain another option in the url hence the ``` ^.+/([^/]*)/$ ``` underneath – Complete Noob Jun 30 '23 at 13:32
  • Okay, makes sense. In PHP, for that section you'd just do something like: https://3v4l.org/om4WH – Chris Haas Jun 30 '23 at 13:57
  • Also, and this is totally a personal preference and a lot of people would consider overkill, but instead of making a bunch of global variables I prefer to quantify mine to a subset of known state using something like a DTO: https://3v4l.org/6a3tn#v8.2.7. If I can't correctly create the DTO, then the problem is on the router side, and then on the consuming side I can be (better) guaranteed that something hasn't messed with the state. – Chris Haas Jun 30 '23 at 14:04
  • Hi Chris, I think I'm getting a bit overwhelmed with the potential solutions and my brain has frazzled a little bit. Would the above however preserve the friendly url structure in the address bar ? – Complete Noob Jun 30 '23 at 14:10
  • Yes, the address bar would be preserved. The `FallbackResource` is basically "if Apache can't determine that a file or directory exists then ask this very specific file instead". No redirects happen, unless that specific file (often called index.php or router.php, but you can name it whatever) actually performs a redirect. Inside that file, the global `$_SERVER['REQUEST_URI']` will hold the URL, minus the domain and protocol. And then instead of apache routing to a specific file, you just require/include that file. The only other difference is that `$_GET` isn't automatically populated for you – Chris Haas Jun 30 '23 at 14:20
  • Ah ok great.. instead of mapping to the kw I could replace that with the category ID.. ok this could work nicely .. Ive got it working using my really long method although now appending apply to the url string doesnt work for some weird reason but can rectify that. I much prefer your method.. thanks so much for the help – Complete Noob Jun 30 '23 at 14:25
  • Awesome. And the way you were doing it wasn't wrong at all, either, just to be clear. Often a single line of htaccess might need to be replaced with several lines of PHP, so you lose that succinctness. For me, I prefer readability and portability, the ability to debug and test, and the ability to just dump variables so I can inspect things manually when they don't go the way I expect, and I find it much easier to do in PHP. But the way you were doing it is very common still, and is the preferred way by many people. – Chris Haas Jun 30 '23 at 14:31
  • Yeah its the way Ive always been used to although it does come with headaches. Ive also created the routes.php file and uploaded and edited the htaccess file to contain FallbackResource inc/routes.php but im just getting a 404 error now – Complete Noob Jun 30 '23 at 14:44
  • @ChrisHaas Any idea why im just getting a 404 on everything when I add the FallbackResource into the htaccess file ? – Complete Noob Jun 30 '23 at 15:30
  • Can you post your updated htaccess? – Chris Haas Jul 02 '23 at 11:14
  • Hi Chris I just have FallbackResource routes.php DirectoryIndex index.php – Complete Noob Jul 02 '23 at 14:04

0 Answers0