1

So I have this regex problem and wondered if anyone could help me out?

If a user visits http://example.com/index.php/ how can i modify/add to my regex to prevent a trailing slash(s) at the end?

also

I currently have a page, called post.php that can be accessed like so http://example.com/reviews/reviewTitle/ and http://example.com/news/newsTitle/

again, how could I prevent this trailing slash?

Below is the regex I have so far:

RewriteEngine on RewriteRule ^reviews/([^/\.]+)/?$ reviews/post.php?title=$1 [L] RewriteRule ^news/([^/\.]+)/?$ news/post.php?title=$1 [L] RewriteRule ^page/(.*) index.php?page=$1 

Note: Im also re-writing http://example.com/index.php?page=1 to http://example.com/page/1 etc, same question, how can I prevent a trailing slash?

Many thanks, I really appreciate any help :)

1 Answer 1

1

Try adding this rule after RewriteEngine on

RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ $1 [R=301] RewriteRule ^reviews/([^/\.]+)$ reviews/post.php?title=$1 [L] RewriteRule ^news/([^/\.]+)$ news/post.php?title=$1 [L] RewriteRule ^page/(.*) index.php?page=$1 

Edited to show full set of rules

Edited 2nd Time Added in

RewriteCond %{REQUEST_FILENAME} !-d 

above new rule to allow directorys to still be accessed without causing an infinite redirect loop

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

4 Comments

Hey thanks for the reply, but it causes example.com/reviews/reviewTitle or example.com/news/reviewTitle to not work at all (404 Not Found) and adding a trailing slash causes an Internal Server Error :/
Thanks again, but it now causes an infinite loop of redirects :/ (Firefox has detected that the server is redirecting the request for this address in a way that will never complete.)
Woo Hoo :D It's working now, although I added a / before the $1 to make it redirect to the root, as it was redirecting to the var/www/ folders. Thank you!
to fix that add RewriteBase / I'll post the full code above for you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.