0

Here's what my htaccess looks like:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www.site.com$ [NC]
RewriteRule .? http://site.com%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_URI} /page1/([^?].*)$ [NC]
RewriteRule /page1/(.*)$ /page1/?$1 [L] # change /page1/stuff to /page1/?stuff
# End rewriting for page1


RewriteCond %{REQUEST_URI} /page2/([^?].*)$ [NC]
RewriteRule /page2/(.*)$ /page2/?$1
# End rewriting for page2



RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/$1 [R=301,NS,L]

Options -Indexes

What I also want is to have it that if a user goes to site.com/page1/stuff or site.com/page1/stuff/more it will add a trailing slash in the address bar and change it to site.com/page1/stuff/ and site.com/page1/stuff/more/ but nothing I tried works. Is there any way to do this without causing a redirect loop?

I want all my urls to have a trailing slash and I see directories already doing that.

EDIT: To clarify, I want the trailing slash to be visible to the user but not the ?

qwertymk
  • 34,200
  • 28
  • 121
  • 184
  • you can add a slash after `/$1` => `/$1/` – Shakti Singh Apr 04 '12 at 12:10
  • @ShaktiSingh: a) These changes don't happen in the address bar (ie the user never see's a `?` in the url) and b) what If the query string ends with a `/` then I don't want another one. – qwertymk Apr 04 '12 at 12:14
  • `RewriteCond %{REQUEST_URI} /page2/([^?].*)$` will NOT match `/page2/?stuff` (as %{REQUEST_URI} in this case will be = `/page2/`). To match QUERY STRING you have to use `RewriteCond %{QUERY_STRING} PATTERN_HERE` -- query string (in this particular example) will be just `stuff`. – LazyOne Apr 04 '12 at 12:20
  • @LazyOne: I tried that and even though it changes it on the server end, the user still doesn't see a trailing slash, see edit – qwertymk Apr 04 '12 at 12:22
  • @qwertymk Try this: `RewriteRule ^page1/(.*[^/])$ /page1/$1/ [L,R=301]`. Change R code (redirect/result code) to whatever you need: 301 = Permanent redirect, 302 = Temp redirect. I recommend to start with 302 and when testing is done change to 301 (as modern browsers do cache redirects which may lead to frustration when testing/debugging). – LazyOne Apr 04 '12 at 12:38
  • Isn't that what the [`R`](http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html) flag was for? – knittl Apr 04 '12 at 12:39
  • Flags explained: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_r – LazyOne Apr 04 '12 at 12:43
  • In any case: similar question (force trailing slash in URL) -- http://stackoverflow.com/questions/1668096/add-trailing-slash-to-urls – LazyOne Apr 04 '12 at 12:48

1 Answers1

0

Thank for that link @LazyOne, anyway this works:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
qwertymk
  • 34,200
  • 28
  • 121
  • 184