mod_rewrite simple redirect and trailing slash

i managed to do the simple redirection but still not happy with it

this is what i have so far

RewriteRule ^/?([a-z]+)$ $1.php [L] 

this redirects domain.com/somepage.php to domain.com/somepage and also works with all relative paths to css, js and images on pages

but I also want to force a trailing slash “/”.

At the moment this redirection doesnt work if I point the browser to domain.com/somepage/

help…

thanks

Just add the slash to your rule


RewriteRule ^/?([a-z]+)[B][COLOR="Red"]/[/COLOR][/B]$ $1.php [L] 

and it works :slight_smile:

If you want to make the trailing slash optional (which I don’t advice), put a ? after the /


RewriteRule ^/?([a-z]+)[B][COLOR="Red"]/?[/COLOR][/B]$ $1.php [L] 

thanks
this only works if i point the browser to domain.com/page/ and even then all my relative CSS and images paths get screwed.

also domain.com/page is 404 in this case

Ideally i want to make it so whatever one types domain.com/page/ or domain.com/page/ they get redrected to domain.com/page/ which in turn redirects to domain.com/page.php

Yes, that’s because the browser thinks /page/ is a directory and will go look for relative links in that directory. There are two possible solutions for this, which are described in the section “Relative Links Are Missing!” on Apache’s mod_rewrite

Yes, if you’d read my previous post carefully you know it would. Please read my previous post again.

As for the .htaccess, try this


# Add slashes to requests without slashes -- external redirect
RewriteCond %{REQUEST_FILENAME}\\.php -f
RewriteRule ^/?([a-z]+)$ /$1/ [L,R=301] 

# show /<page>.php instead of /<page> -- internal redirect
RewriteRule ^/?([a-z]+)$ $1.php [L] 

thanks for your reply, when trying you .htaccess i get Page not Found as well.

also isn’t there any other way apart from setting the base?

Yes, you can use absolute URLs. (i.e. /images/bla.gif instead of images/bla.gif)

Can you please post the complete .htaccess you have now?

this is what i have so far

the trailing slash is forced now but my relative paths to images and css folders are not working

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^domain.com [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_URI} !(.*)/$
 RewriteRule ^(.*)$ http://www.domain.com/$1/ [L,R=301] 

RewriteRule ^/?([a-z\\-]+)$ $1.php [L]

RewriteRule ^/?([a-z\\-]+)/$ $1.php [L]

all seems to be working now, i added / before “images” and “css” in relative paths…!! thanks so much for your help