Forum Moderators: phranque
Is there any way I can get that above htaccess rule to only apply to a specific folder?
RewriteRule ^([^.]+[^./])$ http://example.com/$1/ [R=301,L]
RewriteRule ^(blog/[^.]+)/$ /$1.php [L]
RewriteRule ^blog/([^.]+[^./])$ http://example.com/blog/$1/ [R=301,L]
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^blog/([^.]+[^./])(\.php)?$ http://example.com/blog/$1/ [R=301,L]
See how that works? A request that is either missing the desired slash, or has .php at the end, gets redirected. But now you also need a Condition, because of course there will be internal requests for the with-php form. THE_REQUEST means “what the human visitor (or non-human robot) originally asked for”, as distinct from whatever physical file the server is currently handing out. RewriteCond %{THE_REQUEST} !\.php
RewriteRule \.php$ - [L]
which means “if you’re looking for a .php file, but it’s not because the human visitor directly asked for one, stop right here and go on to the next mod”.
Not Found
The requested URL /home/examplesite/public_html/blog/examplepost/ was not found on this server.]
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^blog/([^.]+[^./])(\.php)?$ http://www.example.com/blog/$1/ [R=301,L]
RewriteRule ^blog/([^.]+[^./])$ http://www.example.com/blog/$1/ [R=301,L]
RewriteRule ^(blog/[^.]+)/$ /$1.php [L]
Strangely, this only happens in Chrome!?
RewriteRule ^blog/([^.]+[^./])$ http://www.example.com/blog/$1/ [R=301,L]
--no, really, you don't--because that was the purpose of the (\.php)? in the immediately preceding rule. A single rule covers two different scenarios. in the eyes of a search engine, will everything still be the same?
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^blog/([^.]+[^./])(\.php)?$ http://www.example.com/blog/$1/ [R=301,L]
RewriteRule ^blog/([^.]+[^./])$ http://www.example.com/blog/$1/ [R=301,L]
RewriteRule ^(blog/[^.]+)/$ /$1.php [L]
example.com/blog/page/ - 301 redirects to example.com/blog/page/
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^blog/([^.]+)\.php$ http://www.example.com/blog/$1/ [R=301,L]
RewriteRule ^blog/([^.]+[^./])$ http://www.example.com/blog/$1/ [R=301,L]
RewriteRule ^(blog/[^.]+)/$ /$1.php [L]
put an htaccess file with that rule in that particular folder.