简体   繁体   中英

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

I want to show URL like below examples:

1) http://www.domainname.com/detail/name/123.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".

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.

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. 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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