简体   繁体   English

无法解决.htaccess网址重写程序,为所有页面提供404

[英]Unable to solve .htaccess url rewriterule, gives 404 for all pages

I want to show URL like below examples: 我想显示如下示例所示的URL:

1) http://www.domainname.com/detail/name/123.html 1) http://www.domainname.com/detail/name/123.html

2) http://www.domainname.com/detail/124.html 2) http://www.domainname.com/detail/124.html

In both URLs I want to show if "name" exist then want to display URL with "name" otherwise without "name". 在两个URL中,我要显示“名称”是否存在,然后要显示带有“名称”的URL,否则不显示“名称”。

1) RewriteRule ^detail/(.*).html$ detail.php?id=$1 [QSA]
2) RewriteRule ^detail/(.*)/(.*).html$ detail.php?id=$2 [QSA]

First rule is working file without "name". 第一条规则是没有“名称”的工作文件。 Second rule is not working and gives 404 for all pages. 第二条规则不起作用,并为所有页面提供404。

Thanks in Advance. 提前致谢。

您只能使用一个规则来处理这两种情况:

RewriteRule ^detail/(?:[^/]+/)?([^./]+)\.html$ detail.php?id=$1 [L,NC,QSA]

The problem you are having, is that the first rule matches both your first and second case. 您遇到的问题是,第一个规则与您的第一种情况和第二种情况都匹配。 Obviously when id is name/123 your application can't handle it. 显然,当id为name/123您的应用程序将无法处理它。 What you want to do is limiting the characters to non-slash characters. 您要做的是将字符限制为非斜杠字符。 After all, that means it can only match the last path segment. 毕竟,这意味着它只能匹配最后一个路径段。 Besides that, force yourself to always escape literal dots in a regex. 除此之外,强迫自己总是在正则表达式中转义文字点。 A dot matches pretty much anything if you don't do that... 如果您不这样做,圆点几乎可以匹配任何东西...

RewriteRule ^detail/([^/]+)\.html$ detail.php?id=$1 [QSA,L]
RewriteRule ^detail/[^/]+/([^/]+)\.html$ detail.php?id=$1 [QSA,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM