1

I need some help with an .htaccess redirect. Here's an example. Some of my webpages will look something like this... https://www.example.com/my-folder-name/my-file-name-12345678.php. Some pages will have 8 digits at the end like that.

I want to make it easy to get to any file who's name ends with 8 digits and .php.

So, in this case, I would be able to get to this file by typing https://www.example.com/my-folder-name/12345678. So, the part of the file name before the 8 digits and the .php extension won't need to be typed. Can that be done with .htaccess?

This would need to effect any file on my site ending with 8 digits.php

1 Answer 1

0

get to this file by typing https://www.example.com/my-folder-name/12345678 So, the part of the file name before the 8 digits and the .php extension won't need to be typed.

Unfortunately, this is not possible in .htaccess (mod_rewrite), since there is no mechanism to essentially search for a file based on a partial name. You would need to search a directory for a file whose name ends with the sequence of digits passed in the URL. (Other server-side scripting languages, like PHP, can do this fairly easily - but if you have many requests/files then it's not going to be very efficient unless you employ a caching/lookup mechanism. I'm also assuming the "number" is unique and maps to a single file, otherwise this is impossible, regardless of scripting language, unless you are happy with a first-match-wins.)

In short, you would need to pass my-file-name in the requested URL. The .php extension is trivial since that is just a static string that can be hardcoded.

Alternatively, if you have access to the server config then you could potentially create a rewrite-map with all the code to filename mappings. This is essentially a lookup table that can be referenced from .htaccess to retrieve the corresponding filename given the code passed in the URL. Although whether this would be practical to create/maintain is another matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.