SEO friendly URLs

I have the following rewrite rule in my .htaccess file

RewriteRule ^([A-Za-z0-9-]+)/?$ $1.php

This works fine, mapping contact-us to contact-us.php etc. The only thing is, it allows a trailing slash eg contact-us/ which maps to contact-us.php/ Although this still works, it screws up relative addressing to CSS and image files. So is there a way to write the rule so it ignores any trailing / ? Thanks

Why would you want to remap all url components to end in .php?

Sorry, I’m breaking my own rule about questioning OP about what they’re doing instead of answering the question. Just curious.

Perhaps I’ve not phrased it well. The website uses urls without the .php and what the .htaccess file is doing is redirecting to the real url which obviously needs the .php on the end…

gandalf,

The URLs are for files and not directories, therefore, the trailing slash is in error as it specifies a directory name (preceding the /). Delete the optional trailing slash to resolve your problem.

Regards,

DK

Yeah, my question is why, out of curiousity, were you wanting to add .php extensions back into your URL? Most people make efforts to remove them, not return them after removed :smiley:

In fact, there’s probably something in your .htaccess removing them already, more likely than not.

Thanks DK. That’s just the job!

@gandalf458, you’re very welcome! I do get rather pedantic over small details and this is one of my bugaboos (pet peeves) so I’m glad that it helped you out of the situation you found yourself in.

Regards,

DK

1 Like

@dklynn it’s often those small details that matter. I’m really pleased to get an answer as it was bugging me. Thanks again!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.