问题原文
原帖链接 · Jayreis · CC BY-SA 4.0
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 · CC BY-SA 4.0
...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_].
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
