1

my www folder has the following .htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

my api folder (within the www folder) has the following .htaccess

RewriteEngine On
RewriteBase /api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /api/index.php [L]

I would like to redirect www to non www urls while keeping the rules for index.php. I tried many things without success ...

What I tried :

https://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www

Update :

I think I might just need a succession of rules

That is :

1/ redirect www to non www url and go to 2

2/ If file does not exist -> redirect to index.php

RewriteEngine On
RewriteBase 
%% Here redirect www to non www url %%
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
9
  • Are you wanting to redirect the "api" requests as well? (Does that matter? Although any requests for the "api" should be to the canonical URL to begin with, since any script making the "api" request is unlikely to follow redirects I would think.)
    – MrWhite
    Jan 3 at 18:08
  • "I tried many things without success ..." - You should include what you have tried. This is a relatively simple redirect that is repeated many times on here and everywhere, so it may be a simple mistake you are making?
    – MrWhite
    Jan 3 at 18:10
  • Yes I would like ....
    – 2WFR
    Jan 3 at 18:10
  • Do you specifically want to maintain two separate .htaccess files?
    – MrWhite
    Jan 3 at 18:12
  • Well it I do not keep those 2 files, my web site stops working.
    – 2WFR
    Jan 3 at 18:14

1 Answer 1

1

From what you've stated in comments, there doesn't seem to be a requirement to maintain two separate .htaccess files. In this case it would be simpler to maintain just one .htaccess file in the document root (assuming they are related).

I am also making the following assumptions about your "api", which also simplifies the directives:

  • All requests of the form /api/<anything> should be routed to /api/index.php
  • There are no physical files (or directories) that need to be accessed via /api/<file>. (It is an "API" after all.)

You can then do something like the following in the root .htaccess file (and removing the /api/.htaccess file entirely).

RewriteEngine On

# Canonical redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

# Prevent further processing if the front-controller(s) has already been requested
RewriteRule ^(api/)?index\.php$ - [L]

# Rewrite requests for the API
RewriteRule ^api/. api/index.php [L]

# Rewrite requests for the root application
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]   

The RewriteBase directive is not required here.

Note that you should first test with a 302 (temporary) redirect to avoid potential caching issues. And you will likely need to clear your browser (and any intermediary) caches before testing.

However, a slight concern with this (as I mentioned in comments) is with implementing a canonical redirect for requests to your /api/. Scripts that are making requests to an API won't generally follow redirects - it is expected that these requests are already being made to the canonical URL. So, implementing a redirect for these requests could potentially break these "incorrect" API calls to the non-canonical hostname. Does it matter if the API calls are made to the non-canonical hostname? The www to non-www redirect is generally only for SEO, which does not apply to an API.


Maintaining two .htaccess files (alternative)

Alternatively, if you are maintaining two separate .htaccess files (maybe they are two unrelated projects). One in the root and the other in the /api subdirectory then you would need to repeat the www to non-www (canonical) redirect in both .htaccess files since mod_rewrite directives are not inherited by default.

For example:

# /.htaccess (root) file

RewriteEngine On

# Canonical redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

# Prevent further processing if the front-controller has already been requested
RewriteRule ^index\.php$ - [L]

# Rewrite requests for the root application
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

and

# /api/.htaccess file

RewriteEngine On

# Canonical redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

# Prevent further processing if the front-controller has already been requested
RewriteRule ^index\.php$ - [L]

# Rewrite everything to the API
RewriteRule . index.php [L]

Unlike your original /api/.htaccess the api subdirectory does not need to be explicitly stated since we are using relative URL-paths (and no need for the RewriteBase directive). A relative URL-path is relative to the directory that contains the .htaccess file - so it's relative to the /api directive in this case.

2
  • 1
    Just have tested. Works like a charm !
    – 2WFR
    Jan 11 at 21:02
  • @2WFR That's great. Just for kicks I added an alternative solution if two .htaccess files were to be maintained.
    – MrWhite
    Jan 11 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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