Score:1

Redirect www to non www and redirect to index.php

gb flag

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]
kz flag
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.)
kz flag
"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?
chaL67 avatar
gb flag
Yes I would like ....
kz flag
Do you specifically want to maintain two separate `.htaccess` files?
chaL67 avatar
gb flag
Well it I do not keep those 2 files, my web site stops working.
kz flag
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?
kz flag
Incidentally, the code in the linked SO question should have worked in the root `.htaccess` file, but not in the `/api/.htaccess` file.
djdomi avatar
za flag
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)
chaL67 avatar
gb flag
Do not think so :-( I guess I need to separate and sequential rules (see edit).
Score:1
kz flag

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.

chaL67 avatar
gb flag
Just have tested. Works like a charm !
kz flag
@2WFR That's great. Just for kicks I added an alternative solution if two `.htaccess` files were to be maintained.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.