1

I have the follow URLs:

www.website.com/index.php?section=index www.website.com/index.php?section=freebies www.website.com/index.php?section=deals www.website.com/index.php?section=articles 

and the following pagination url parameter for each: page=1 for example

www.website.com/index.php?section=freebies&page=1 

I am using the rewrite rules below to change the URL to:

www.website.com/freebies/1 etc 

www.website.com/freebies/1 and www.website.com/freebies/ work correctly but if I remove the trailing slash for example:

www.website.com/freebies 

It is showing www.website.com/freebies/?section=freebies&page=1

How can I stop this from happening.

My rewrite rules:

RewriteRule ^([0-9]+)/?$ index.php?section=index&page=$1 [NC,L] RewriteRule ^freebies/?$ index.php?section=freebies&page=1 [L] RewriteRule ^articles/?$ index.php?section=articles&page=1 [L] RewriteRule ^deals/?$ index.php?section=deals&page=1 [L] RewriteRule ^freebies/([0-9]+)/?$ index.php?section=freebies&page=$1 [NC,L] RewriteRule ^articles/([0-9]+)/?$ index.php?section=articles&page=$1 [NC,L] RewriteRule ^deals/([0-9]+)/?$ index.php?section=deals&page=$1 [NC,L] 

2 Answers 2

1

freebies appear to be a real directory and Apache's mod_dir module adds a trailing slash for directories causes a redirect thus exposing your internal URL to clients.

You can use:

RewriteEngine On # internally add a trailing slash to directories RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L] RewriteRule ^([0-9]+)/?$ index.php?section=index&page=$1 [NC,L,QSA] RewriteRule ^freebies/?$ index.php?section=freebies&page=1 [L,NC,QSA] RewriteRule ^articles/?$ index.php?section=articles&page=1 [L,NC,QSA] RewriteRule ^deals/?$ index.php?section=deals&page=1 [L,QSA,NC] RewriteRule ^freebies/([0-9]+)/?$ index.php?section=freebies&page=$1 [NC,L,QSA] RewriteRule ^articles/([0-9]+)/?$ index.php?section=articles&page=$1 [NC,L,QSA] RewriteRule ^deals/([0-9]+)/?$ index.php?section=deals&page=$1 [NC,L,QSA] 
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou, this was the problem, I have a freebies, deals and articles folder, but didn't realise it would behave this way. Thankyou :)
1

If i understand correctly, you need the trailing slash always, so your rules always work.
If that's the case, then above your existing rules, add this:

RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301] 

Or if this doesn't work for you, check other techniques for adding trailing slash.

Side tip:
Your existing htacces rules

RewriteRule ^freebies/?$ index.php?section=freebies&page=1 [L] RewriteRule ^articles/?$ index.php?section=articles&page=1 [L] RewriteRule ^deals/?$ index.php?section=deals&page=1 [L] 

Can be simplified by just one rule:

RewriteRule ^(freebies|articles|deals)/?$ index.php?section=$1&page=1 [L] 

EDIT:
It is possible to do what you ask (work with slash and without slash). I think something like this will do the trick (untested):

RewriteRule ^freebies/?$ index.php?section=freebies&page=1 

(note the ? after the slash)

Also, read https://webmasters.googleblog.com/2010/04/to-slash-or-not-to-slash.html) to check how the G treats these scenarios.

3 Comments

Thanks for the quick response :) Is it possible to do this without forcing trailing slashes. So for example that 'www.website.com/freebies' and 'www.website.com/freebies/' both redirect to 'index.php?section=freebies&page=1'
RewriteRule ^freebies/?$ index.php?section=freebies&page=1 Is the RewriteRule I am using, but I am having the problem that when I go to: 'www.website.com/freebies', the URL is appearing as 'www.website.com/freebies/?section=freebies&page=1' in the browser. It only works correctly if I got to 'www.website.com/freebies/' with the trailing slash?
Anubhava's answer solved the problem, thankyou for the simplified ReWrite Rule though :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.