1

Writing a little CMS.

http://mydomain.com/(somefolder)/ should become

http://mydomain.com/(somefolder)/filename

and

http://mydomain.com/(somefolder)/(somefolder2)/ should become

http://mydomain.com/(somefolder)/(somefolder2)/filename

and so on recursively. If recursion is too involved, I'd be happy with the two example directory levels.

filename is some fixed string I'll type into .htaccess when I decide what it should be :)

A bonus would be if I can save one redirect if the directory url is lacking a trailing /. Possible?

1 Answer 1

1

You can use this rule in your root .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?)/$ /$1/filename [L]
Sign up to request clarification or add additional context in comments.

2 Comments

This is correct. However I'm using canonical URLs that sometimes end with /, and those URLs are also affected. Can I add a conditional check that (somefolder) must exist for the rewrite rule to take effect?
I dispute that was 5 minutes, since it said 3 minutes ago when I pressed Save Edits. Anyway. If you're using canonical URLs that sometimes end with /, those URLs are also affected. To avoid 404s on these, add RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+?)/$ /$1/ [L]. If only a subtree of the folders should be affected, put all 5 lines in the .htaccess in the appropriate base folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.