tags:

views:

132

answers:

1

I want all my pages to work if the user added a trailing slash in the end or not

the following line works

RewriteRule ^index/page/([0-9]+)/?$ /cmstut/index.php?page=$1 [QSA,L]

but the the following line cause an internal server error, It's the last line That's the line that should rewrite all other pages which have no attributes like the contact page, about us page or index page

RewriteRule ^(.+[^/])/?$ /cmstut/$1.php [QSA,L]
+1  A: 

You should better use just one spelling (with or without trailing slash) and redirect if the requested URI path is incorrect:

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]

# remove trailing slash
RewriteRule (.+)/$ /$1 [L,R=301]
Gumbo
I see, I think that would be best.so I copy the add trailing slash or the remove trailing slash, but not both right?which, in your opinion is better, with or without?
krike
@krike: Yes, you would only need one of the two rules. Otherwise you will get an infinite recursion loop. And I would prefer the one without the trailing slash.
Gumbo
So I replacedRewriteRule ^(.+[^/])/$ /cmstut/$1.php [QSA,L]withRewriteRule (.+)/$ /cmstut/$1 [L,R=301]if I browse for http://localhost/cmstut/tutorial/65/ shouldn't it remove the last / ??I can now both browse http://localhost/cmstut/tutorial/65/ and http://localhost/cmstut/tutorial/65 and have no internal server error :) so in a way it's fixed, if you could just clarify my question above
krike
krike
@krike: Try the logging feature (see RewriteLogLevel) to track down the cause.
Gumbo
127.0.0.1 - - [24/Nov/2009:21:51:34 +0100] [localhost/sid#905150][rid#12e1600/initial] (3) [perdir I:/wamp/www/cmstut/] strip per-dir prefix: I:/wamp/www/cmstut/ -> 127.0.0.1 - - [24/Nov/2009:21:51:34 +0100] [localhost/sid#905150][rid#12e1600/initial] (3) [perdir I:/wamp/www/cmstut/] applying pattern '^shop/item/(.*)/?$' to uri ''this is one of the lines with the shoplocalhost/cmstut/index has for example "to uri 'index.php'"and all other pages have a page in the "to uri"
krike
Ok I see my answer is hard to read, but actually there is no page between the quotes at "to uri" for the shop while other pages have something between the quotes at "to uri"
krike
@krike: When using mod\_rewrite in a .htaccess file, the per-directory prefix is stripped before testing the rules. In your case you seem to request `/cmstut/` where `/cmstut/` is also the prefix that is stripped.
Gumbo
euhm.... this is my htaccess code, I guess that will be easier for you to see. what am I doing wrong ?http://cmstutorials.org/htaccess_online.txt
krike
@krike: Always put those rules that cause an external redirect before those that cause internal redirects/rewrites. Another issue is that `RewriteCond` directives define conditions only for the very next `RewriteRule`.
Gumbo
I've put the external redirects between comments and they are on before the intrnal rewrites.about the rewriteCond... ok but what do I do now? I have no idea what I should do.
krike
@krike: You could use a break rule that ends the rewriting process if an existing file or directory is requested: `RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L]`
Gumbo
I changed it back to RewriteRule ^(.+[^/])/?$ /cmstut/$1.php [QSA,L] and it works again. so I guess something wrong. I get only an internal server error for pages that does not exist... weird...so for now it works but I'll let you know if I there is still some bugs.
krike