3

I have a folder structure like the below www.exampe.com/distributor/index.php and I want to access the file via an seo friendly www.example.com/distributer/abcinc and then in the index.php want to get the value of the last segment in this case abcinc into the page as a variable

My issue is I am getting a 404 when I go to www.example.com/distributer/abcinc but the page loads if I go to www.example.com/distributer

Here is my .htaccess file which lives in the distributor folder

RewriteEngine On
RewriteRule ^distributor/([0-9]+)$ distributor/index.php?id=$1

I confirmed via phpinfo() that mod_rewrite is all setup/enabled so not sure what I am missing

MrWhite
  • 12,647
  • 4
  • 29
  • 41
Jayreis
  • 145
  • 15

2 Answers2

4

...my .htaccess file which lives in the distributor folder

RewriteRule ^distributor/([0-9]+)$ distributor/index.php?id=$1

If the .htaccess file is actually inside the /distributor directory then you should not be referencing the distributor directory in the RewriteRule pattern. This matches against a URL-path that is relative to the directory that contains the .htaccess file.

Likewise, you should not include the distributor directory in a relative substitution string when inside the /distributor directory. A relative substitution is again relative to the directory that contains the .htaccess file.

Your existing .htaccess file is written as if it was located in the document root directory, not the /distributor subdirectory as stated.

However, you are also referencing both "distributor" and "distributer" in your examples - I assume the "e" is a typo, but this will obviously also result in a failed match.

And, as mentioned in @TeroKilkanen's answer, you are matching digits in your regex, not letters, as in your example URL.

So, your existing rule is matching a URL of the form /distributor/distributor/123 and rewriting to /distributor/distributor/index.php?id=123.

Try the following instead, in the /distributor/.htaccess file:

RewriteRule ^(\w+)$ index.php?id=$1 [L]

The \w shorthand character class matches both digits and letters and is equivalent to the character class [0-9a-zA-Z_].

MrWhite
  • 12,647
  • 4
  • 29
  • 41
2

Your RewriteRule matches only numbers, that is, distributor/3728 etc. It does not match abcinc.

From the id argument passed to index.php it looks like your application expects an ID here and not a name. You need to check your application how to set it up to handle names.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • 1
    even if I make the url be https://warrantycenter.cross.com/distributor/12 it gives me a 404. Although I will change it to [a-z] – Jayreis Aug 31 '22 at 18:12