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]
2WFR
  • 135
  • 1
  • 7
  • 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 03 '23 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 03 '23 at 18:10
  • Yes I would like .... – 2WFR Jan 03 '23 at 18:10
  • Do you specifically want to maintain two separate `.htaccess` files? – MrWhite Jan 03 '23 at 18:12
  • Well it I do not keep those 2 files, my web site stops working. – 2WFR Jan 03 '23 at 18:14
  • But you don't _need_ two separate files (unless you have a specific requirement). You can do everything (more simply) in just one file. Also, what type of requests are you expecting to your "api"? The filesystem checks are probably unnecessary here. Is a request for `/api/some-file.ext` a legitimate request? – MrWhite Jan 03 '23 at 18:31
  • Incidentally, the code in the linked SO question should have worked in the root `.htaccess` file, but not in the `/api/.htaccess` file. – MrWhite Jan 03 '23 at 18:34
  • Does this answer your question? [How to get Apache2 to redirect to a subdirectory](https://serverfault.com/questions/9992/how-to-get-apache2-to-redirect-to-a-subdirectory) – djdomi Jan 03 '23 at 19:12
  • Do not think so :-( I guess I need to separate and sequential rules (see edit). – 2WFR Jan 03 '23 at 19:48

1 Answers1

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.

MrWhite
  • 12,647
  • 4
  • 29
  • 41