Mike West
Mike West

Reputation: 35

mod_rewrite / regex with unknown folder name

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?

Upvotes: 0

Views: 73

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133538

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]

Upvotes: 1

Related Questions