简体   繁体   English

如何使用和不使用斜杠来修复htaccess

[英]How to fix htaccess with and without trailing slash

Hello I am facing a very little problem, when i browse site without trailing slash it works fine but when i add trailing slash in the end it redirect me to 404 page 您好,我遇到了一个小问题,当我浏览网站时不使用斜杠时,它可以正常工作,但是当我最后添加斜杠时,它会将我重定向到404页

eg 例如

mysite.com/category/slug-name/page/3/ mysite.com/category/slug-name/page/3/

mysite.com/category/slug-name/page/5/ mysite.com/category/slug-name/page/5/

works fine but when i click page 1 it that has a trailing slash ie 工作正常,但当我单击第1页时,它带有斜杠即

mysite.com/category/slug-name/ mysite.com/category/slug-name/

it gives error but without trailing slash it works fine for me 它给出了错误但没有斜杠对我来说很好

Similarly on articles page when i access without trailing slash it works but when i add trailing slash in the end, It redirects me to 404 page eg when i access this URL it works fine 同样,在文章页面上,当我访问时不带斜杠就可以工作,但是当我在末尾添加斜杠时,它会将我重定向到404页,例如,当我访问此URL时,它就可以

mysite.com/123-article-slug-here mysite.com/123-article-slug-here

but in this case I got 404 error 但在这种情况下,我收到404错误

mysite.com/123-article-slug-here/ mysite.com/123-article-slug-here/

I am sure that there is a problem in my htaccess but know how to fix it 我确定htaccess中存在问题,但知道如何解决

RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9-/]+)/page/(.+)/?$ category.php?id=$1&page=$2 [NC,L]
RewriteRule ^category/([a-zA-Z0-9-/]+)/?$ category.php?id=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+)/?$ article.php?url=$1

Try either removing the / from the slug name regex grouping or make that match non-greedy: 尝试从子名称regex分组中删除/或使该匹配不贪心:

Remove / from grouping: 从分组中删除/

RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9-]+)/page/(.+)/?$ category.php?id=$1&page=$2 [NC,L]
RewriteRule ^category/([a-zA-Z0-9-]+)/?$ category.php?id=$1 [L]
RewriteRule ^([a-zA-Z0-9-]+)/?$ article.php?url=$1

Make non-greedy: 设为非贪婪:

RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9-/]+?)/page/(.+)/?$ category.php?id=$1&page=$2 [NC,L]
RewriteRule ^category/([a-zA-Z0-9-/]+?)/?$ category.php?id=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+?)/?$ article.php?url=$1

What's probably happening is that the / is getting matched inside the grouping (the parens) and that is getting passed to your category.php , and it doesn't like the / so returns a 404. 可能发生的是/在分组(括号)内匹配,并且正在传递给您的category.php ,并且它不喜欢/因此返回404。

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

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