0

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

e.g.

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

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

works fine but when i click page 1 it that has a trailing slash i.e.

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 e.g. when i access this URL it works fine

mysite.com/123-article-slug-here

but in this case I got 404 error

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

I am sure that there is a problem in my htaccess but know how to fix it

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 

1 Answer 1

1

Try either removing the / from the slug name regex grouping or make that match non-greedy:

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome Non-greedy works for me thanks but can you please tell me what is the difference between removing / from grouping and non-greedy
@Brajman Removing the / from the grouping means you can't have / in your slug names, so: /category/this/is/my-slug/ won't work. But if you make the match non-greedy, you can still match the / but it just won't match the optional slash at the end.
Oh thats a great info for me. Really really Thanks once again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.