0

I've read through about 6 or 7 suggested similar post but still can't get this working.

(e.g.: Reference: mod_rewrite, URL rewriting and "pretty links" explained / How to write htaccess rewrite rule for seo friendly url etc...)

I have a PHP website with a help articles section, which pulls the content from the db by article id. Current URL format =

/sitename/help.php?h=69

I'd like to change the URL format to:

/sitename/help/69/title-of-the-article

Mod_rewrite is enabled on the server, and this seems to work:

RewriteRule ^help/([0-9]+)/?(.*)/?$ /sitename/help.php?h=$1 [NC,L]

/sitename/help/69/random-title loads the content for /sitename/help.php?h=69

However, some of the articles contain images, coming from the path /sitename/images/ The modified URL means the browser tries to call /sitename/images/example.jpg from sitename/help/69/images/example.jpg

The '69' is an unknown variable, (and that seems to break all the examples I've tried!), but should always be a 1, 2 or 3 digit number. Please can someone help with the rewrite/regex query to remove the /help/{article id}/ from my image URLs?

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Mike West
  • 15
  • 5

1 Answers1

1

With your shown samples and attempts please try following .htaccess rules file. Please make sure to keep your .htaccess rules file along with sitename folder(not inside it, alongside it). Also clear your browser cache before testing your URLs.

RewriteEngine ON

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sitename/(help)/(\d+)/.+$  sitename/help.php?h=$1 [NC,L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • no luck I'm afraid. Please could you explain what your code is trying to do? – Mike West Sep 14 '22 at 11:06
  • @MikeWest, Its checking for non-existing urls and if a url starts from `sitename` followed by `help` and any digits then any thing it rewrites it to help.php in backend. – RavinderSingh13 Sep 14 '22 at 11:07
  • So is it only doing the help redirect, not the image URL correction? – Mike West Sep 14 '22 at 11:26
  • @MikeWest, ok, could you please do let me know **FROM** which URL **TO** which url you want to redirect, a meaningful sample will be helpful for me to understand it better, cheers. – RavinderSingh13 Sep 14 '22 at 13:26
  • Ok, assume sitename is the domain. Current structure is: `sitename/help.php?h=[numeric article id]` I want it to be: `sitename/help/[article id]/[article title]` `RewriteRule ^help\/([0-9]+)\/?(.*)\/?$ /sitename/help.php?h=$1 [NC,L]` ...works, but any images and links in the articles break, as they are now relative to /sitename/help/[id]/` rather than `/sitename/` – Mike West Sep 14 '22 at 16:43
  • @MikeWest, oh ok but from where `article title` is coming I can't see its coming from help.php? – RavinderSingh13 Sep 14 '22 at 16:49
  • My plan is to append those to the links via php, and reload the page, (hence the `\/?(.*)\/?` part of the rule I posted) – Mike West Sep 14 '22 at 18:02
  • @MikeWest, these htaccess rules are for rewriting in backend just for your FYI please!! – RavinderSingh13 Oct 18 '22 at 06:45