Reputation: 822
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]
Upvotes: 1
Views: 1863
Reputation: 784998
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]
Upvotes: 1
Reputation: 2098
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.
Upvotes: 1