1

I set up my website so that all pages load without the .html extension, using the following code in the .htaccess file:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

It works beautifully on the main site. The problem is that I have a few subdomains on the same site, and this change has been disastrous for those pages, with files and folders loading in ways they shouldn't.

Is it possible to do either of the following?:

a) Set up the above code to only target specific pages? This would be preferable, I think, if it's possible to do. Only 9 pages in the main site need load without the .html extension. So for example, maybe one of those "RewriteCond"s could be:

RewriteCond services.html -f

(And I have no idea if that's correct code - it's just an example of one page that needs to load without the .html extension.)

b) Alternately, could the above .htaccess code be rewritten to ADD the .html extension back in? Maybe I could add that to the subdomains' individual .htaccess files, and hopefully it would "overwrite" the main .htaccess code.

Hope someone who is more fluent at .htaccess than I am has some ideas! Thanks in advance.

Jen

EDIT: Solution found! Thanks to some new terms given in the comments (especially mod_rewrite), I did more searching and found that the following code works to enable non-.html links:

RewriteRule ^page$ /page.html [L,E=END:1]
RewriteCond %{ENV:END} !1
RewriteRule ^page.html$ /page [R=301,L]

The above code enables links to work without the .html extension. The only catch is that links that have the .html extension will still load WITH the extension (it's not removed automatically) but that's a minor issue to me. I just wanted to be able to use nicer-looking links.

Credit to my commenter Stephen, who actually solved this question years ago at: Remove extension from URL using a rewrite without resulting in a redirect loop

14
  • Two generic comments, but first you did not specific where these rules appear and how they apply to all your sites. In general: 1) it is far easier to have RewriteRules in the main apache configuration files, instead of in .htaccess files for security, performance and simplicity reasons, and 2) for very simple needs, and specially because you are saying you need the feature for only 9 pages, look at simpler stuff like mod_alias and its Redirect directives. I also suggest not matching THE_REQUEST as this is far too much for the simple needs you have... Jul 9, 2019 at 22:51
  • You may also wish to expand on "this change has been disastrous for those pages, with files and folders loading in ways they shouldn't. " What does that mean exactly? What did you test? What URLs did you use? What was the result? What did you expected instead? and so on... Jul 9, 2019 at 22:52
  • Thank you, Patrick. I will search for info on mod_alias and Redirect. I used htaccess because that's more familiar - I have never worked with apache config. Sorry for not being clearer on the subdomain issues. Example: one of the subdomains has a directory called "photos" and a page that sits alongside called "photos.html." With the code above, a browser request for the photo.html page returned a page listing the "photos" directory files instead. Since the subdomain has many setups like that, the problem needs fixing.
    – Jen
    Jul 10, 2019 at 0:26
  • "a browser request for the photo.html page returned a page listing the "photos" directory files instead." This is because of your last 2 lines: you are saying "if anything ends in .html, just strip it, and 301 redirect to new URL", so yes "photos.html" is rewritten to just "photos" and then Apache detects it is a directory and mod_autoindex or equivalent jumps in. Anyway, make sure to have your .htaccess file in the specific subdirectory of the virtual host you want it to apply, and not for all virtual hosts. And I recommend reworking your last 2 lines of configuration. Jul 10, 2019 at 0:47
  • 1
    Why don't you just move the subdomains to a different document root and change your virtual hosts? That way they won't share .htaccess rules with your main domain. Jul 10, 2019 at 1:45

1 Answer 1

1

The problem is that I have a few subdomains on the same site, and this change has been disastrous for those pages...
:
The problem is that the .htaccess file for the main site also controls the subdomains. The subdomains have their own .htaccess files

By the sounds of it, your subdomains map to subdirectories off the main domain's document root. In this case, you can simply enable (or disable) the rewrite engine in the subdomains .htaccess file in order to prevent the mod_rewrite directives in the parent config being inherited (ie. executed). (mod_rewrite directives are not inherited by default.)

For example, in the subdomains .htaccess file:

# Disable the rewrite engine
RewriteEngine Off

Alternatively, change the document root of the subdomains to point to a different area of the filesystem (as @Stephen suggested in comments) - so the main domains .htaccess file is not processed.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.