Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Another htaccess fool

8:17 pm on Nov 1, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 16, 2002
votes: 1

I am having an issue with sub directories and pretty uls. I have tried a bunch of code variants and obviously do not have the knowledge. Perhaps someone will help me out. I am wanting to have sub directories with files and this only works if all files are in the root. How to allow for pretty urls in sub directories please. This is my current file sans my code that does not work:

Options +FollowSymLinks -MultiViews 

RewriteEngine on

#Redirect non-canonical to www
RewriteCond %{HTTP_HOST} !^(www\.mydomain\.fr)?$
RewriteRule (.*) http://www.mydomain.fr/$1 [R=301,L]

RewriteCond %{HTTP} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#Redirect index.html and .htm to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1.php [L]

[edited by: phranque at 9:52 pm (utc) on Nov 1, 2017]
[edit reason] code tags [/edit]

9:32 pm on Nov 1, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
votes: 598

Lesson #1: Put all your examples into [ code ] markup to avoid auto-linking. Use “example.com” (or “example.fr” or any TLD you like) whenever you mention your site name.

Your first three rules are in the wrong order, and there are too many of them. The www and https rules can be combined into a single domain-name-canonicalization redirect, with two different Conditions separated by [OR]. This is the very last redirect. The "index" redirect normally goes immediately before the canonicalization redirect. And, unless you have very strange URLs, you can say index\.htm without closing anchor.

Please don't bother with the -f and -d tests when it is your own site and you're not using a commercial CMS. It's just extra work for the server. (Besides, you don't need -f at all if page URLs all end in / slash; you only need to make sure it isn't a real directory.) Instead, list your directories by name: preferably in the body of the rule, but otherwise as a negative condition. For example
RewriteRule ^(fakedir|otherfake|thirdfake)/(.+)/$ /$1/$2.php [L]
RewriteCond %{REQUEST_URI} !/(realdir|otherreal|thirdreal)
RewriteRule ^(.+)/$ /$1.php [L]
Do not copy-and-paste; I'm making suggestions off the top of my head.

Always put a / at the front of a Rewrite target. It's safer than using a RewriteBase.

If your pretty URLs end in a / you will also need a redirect for requests that do not have a final / slash. (Search engines will make these requests. So will humans, by mistake.) For real directories, mod_dir takes care of it; for fake directories you need to code a redirect. It should go right next to the "index.html" redirect--before or after doesn't matter.