Replaced ".htm" with trailing slash, now need to create exception

A few wks back, I was looking for the regex in .htaccess to replace “.htm” in a URL with a trailing slash. With that figured out, everything seemed great. Now, though, I need to create an exception.

The one exception I’m looking to make is to only replace the “.htm” with a trailing slash if there is no query string after the .htm. So, “www.url.com/more/links.htm” would become “www.url.com/more/links/”, but “www.url.com/more/links.htm?page=1” would be untouched.

If it’s not possible to check for that condition, I’d be willing to compromise by checking for a string in the directories leading to “.htm”. If there is “/admin/editor/” in the URL, don’t touch it, but if it’s anything else, feel free to mess with it.

This is my existing simple trailing slash-.htm swap:

RewriteRule ^(.+)\.htm$ http://www.navaatlasveganrecipes.com/$1/ [L,R=301]

Judging by my programming knowledge, but lack of htaccess/regex experience, my stab @ it would go like:

RewriteRule ^!(\\/admin\/editor\/\)(.+)\.htm$ http://www.navaatlasveganrecipes.com/$1/ [L,R=301]

You need two ingredients: RewriteCond and %{QUERY_STRING}.

I’ll leave it up to you to figure out the recipe okay? But feel free to come back if you don’t get it of course :slight_smile:

I get what you’re saying Scallio, though not 100%. Is it absolutely necessary to use a RewriteCond?

What about doing something like this:

RewriteRule ^?!/wp-includes/js/tinymce/themes/advanced/(.+)\.htm$ http://www.navaatlasveganrecipes.com/$1/ [L,R=301]

Meaning: “Don’t match ‘/wp-includes/js/tinymce/themes/advanced/’ AND match ‘anything.htm’”

If you’re aiming for negative lookahead it should be:


RewriteRule ^(?!/wp-includes/js/tinymce/themes/advanced/)(.+)\\.htm$ http://www.navaatlasveganrecipes.com/$1/

Yes, you could that, but negative lookaheads are pretty expensive and if I were you I’d stay well clear of them unless there is no other solution.

In this case there is:


RewriteCond %{REQUEST_URI} ^!/wp-includes/js/tinymce/themes/advanced
RewriteRule (.+)\\.htm$ http://www.navaatlasveganrecipes.com/$1/

Of course if you want to exclude more directories, just add more RewriteCond’s in the same fashion as the one above.

To come back to the query string, the RewriteRule cannot check the query string, so if you’re not checking by the path the html is in, you have to use RewriteCond.

PS. If your files don’t contain any other dots than the dot in .html, change (.+) to ([^.]+). (.+) is greedy and will eat everything it sees, and that oftentimes causes problems. ([^.]+) will match anything except for a literal dot character, thus it also stops when it finds the . in .html
The ([^.]+) is also slightly less expensive for the regex engine.

Thank you Scallio. I’ve got it to work, it was a pain in the ass, but I remembered to escape the fwd slashes and add the [L,R=301] after some solid troubleshooting. Your teaching style is both non-handicapping, enlightening, and just enough guidance so I can learn to think in the right way to get it done. I appreciate it.

CELEBRATION OVER, EVERYBODY GO HOME!

Now that I’ve gotten the chance to try it again, it doesn’t seem to work.

This is the code I’m using:

RewriteCond %{REQUEST_URI} ^!/wp-includes/js/tinymce/themes/advanced [NC]
RewriteRule ^(.+)\.htm$ http://www.vegkitchen.com/$1/ [L,R=301]

Somewhere along the line, it’s just broken, because it doesn’t not replace the .htm with a trailing slash.

Hold on, did I write that!? Must’ve been tired :x. The ^ and ! are supposed to go the other way around.
Also, do you have variants of the /wp-includes/js/tinymce/themes/advanced directory where some of the letters are capitals instead of lower case, like /WP-includes/JS/TinyMce/Themes/Advanced ?
If you don’t, ditch the [NC] :slight_smile:

Rémon,

Why not change the (.+)\.html$ to ([^/]+)\.htm$ ?

Regards,

DK

If we take the example URL:

http://www.example.com/information[COLOR=“Red”]/[/COLOR]contact[COLOR=“Blue”].htm[/COLOR]

The OP stated that should be rewritten to:

http://www.example.com/information/contact[COLOR=“Blue”]/[/COLOR]

If we were to use ([^/]+) the regex engine would cop out at the / between information and contact and the RewriteRule wouldn’t match.

Which is why I suggested ([^.]+) but of course that doesn’t work if there are dots anywhere in the string that are not the dot in the .html

eg, using ([^.]+) on

http://www.example.com/contact[COLOR=“Red”].[/COLOR]us.htm

won’t work …

Scallio, thanks for the guidance. It works like a charm.

You’re welcome. Glad I could help :slight_smile: