大网数据广告活动
服务器运维问答

SEO friendly URL not working

· 52CE 我爱测

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 segmen…

问题原文

原帖链接 · 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_].

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-08-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。