I am trying to setup my .htaccess file to allow for multiple vanity URLs which is fine, but I'm running into issues with them with the pages being in different directories.
First vanity URL is setup and working fine, which works like this:
example.com/blognumber1 > example.com/inspiration/article.php?id=blognumber1
RewriteBase /inspiration/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ article.php?id=$1 [QSA,L]
I am trying to add another one which would work like this:
example.com/supplier/test1 > example.com/profile.php?id=test1
Because the article.php page is in a different directory, I am having problems trying to code it, any help would be appreciated.
Don't use a
RewriteBasedirective (which applies to the entire file) and make sure the more specific/supplier/test1rule is first. For example:Note that the
NCflag is not permitted on theRewriteConddirective when used with the filesystem checks-fand-d. If you check your error log you will probably see a stack of warnings.On the
/supplier/test1rule, it only matches a single path segment after/supplier/, ie. it does not match/supplier/test1/something.Your existing rule is rather generic as it literally matches anything. Perhaps restrict this to a single path segment also? Or restrict the regex to what constitutes a blognumber.
Optimisation
As a further optimisation you could also abort early if a
.phpfile (or a URL that looks-like a.phpfile) is requested. Unless you have valid "supplier" or "blog" URLs that end in.php? For example, immediately after theRewriteEnginedirective:You can extend this for any URL that looks-like it contains a file extension, which should naturally include your static assets and therefore avoid the filesystem check. For example: