2

I have the next file structure:

.htaccess
index.php
public/
├─ v1/
│  ├─ .htaccess
│  ├─ index.php
├─ index.php

I've set document root on server level to public subfolder in order to remove it from url.

I need all the requests containing v1 in url to be redirected to v1/index.php.

My root .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

and v1/.htaccess:

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d  [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ index.php [QSA,L]

Expected behavior:

Non-existing https://example.com/v1/test page should redirect to https://example.com/v1/.

What I get instead:

Not Found
The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

I am pretty sure the problem is in second .htaccess. I also suspect that custom Document Root can be a problem too here.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Acidon
  • 1,294
  • 4
  • 23
  • 44

1 Answers1

5

Try having your root .htaccess in following manner once and give it a shot and remove your inner .htaccess(keep backup of it in your local system though but don't have it on system). Clear your brorwser cache before testing your URLs.

RewriteEngine ON

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} !\s/public(?:/\S*)*\s [NC]
RewriteCond %{THE_REQUEST} !\s/v1(?:/\S*)*\s [NC]
RewriteRule ^(.*)/?$ /public/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} !\s/v1(?:/\S*)*\s [NC]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{THE_REQUEST} !\s/v1/?\s [NC]
RewriteRule ^(v1)/.+/?$ /$1/? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^v1/?$  public/v1/index.php [QSA,L]

NOTE: Will add detailed explanation in morning time as its too late night for me.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • I've tried the code (fixed the typo in folder name in root htaccess), but still getting the same error when open non-existent page within `v1` subfolder, and no redirect when open existing one. – Acidon Oct 19 '22 at 19:29
  • @Acidon, Ok, I have made a small change now, could you please do check my latest code(2nd htaccess file) and try it out once, let me know how it goes. – RavinderSingh13 Oct 19 '22 at 19:32
  • Sadly, the result is still the same. – Acidon Oct 19 '22 at 19:34
  • @Acidon, Is it redirecting url? when you hit that http://localhost:80/v1/test1? – RavinderSingh13 Oct 19 '22 at 19:36
  • @Acidon, I have made 1 more edit to my 2nd htaccess could you please do check it once and let me know how it goes? – RavinderSingh13 Oct 19 '22 at 19:37
  • @RivinderSingh13, still same, when I go to `https://example.com/v1/test1` it will give Not Found error if the page test1.php does not exist in the folder, or it will just open the page if it exists. – Acidon Oct 19 '22 at 19:41
  • @Acidon, oh ok, I have added one more condition in root .htacces now, kindly update both files and let me know how it goes. Make sure to clear browser cache before testing your urls. – RavinderSingh13 Oct 19 '22 at 19:44
  • Still same, and now `v1/index.php` shows Not Found error too. – Acidon Oct 19 '22 at 19:47
  • @Acidon, I had removed one line from v1 htaccess, kindly try it once and let me know if this helps you. – RavinderSingh13 Oct 19 '22 at 19:49
  • Same as the last time - no redirect, also `https://example.com/v1/index.php` works but `https://example.com/v1/` doesn't. – Acidon Oct 19 '22 at 19:55
  • @Acidon, Sorry for same, I am trying my best to help here. I have made edit in my rules now, could you please do check with new rules once and let me know how it goes? Make sure to place newly posted rules and clear cache before testing your URLs. – RavinderSingh13 Oct 19 '22 at 19:58
  • It redirects, but to the url containing `public` subfolder in url. `public` has to be hidden all the time. – Acidon Oct 19 '22 at 20:03
  • @Acidon, ok sure, applied 1 more patch to code, could you please do test with newly edited code and let me know how it goes. – RavinderSingh13 Oct 19 '22 at 20:05
  • 1
    Still `https://example.com/v1/test1` redirects to `https://example.com/public/v1/`... I really appreciate you helping me with this one. – Acidon Oct 19 '22 at 20:10
  • @Acidon, not an issue, trying best to help here. I have changed rules now, kindly try with new rules and check, make sure to clear your browser cache before testing your URLs. – RavinderSingh13 Oct 19 '22 at 20:13
  • same as last time and now `https://example.com/v1/` redirects to `https://example.com/public/v1/` too. – Acidon Oct 19 '22 at 20:18
  • actually it shows 404 for `https://example.com/v1/` (forgot to fix your typo in v1 folder name) – Acidon Oct 19 '22 at 20:21
  • @Acidon, is it redirecting now without public folder name in it? – RavinderSingh13 Oct 19 '22 at 20:22
  • nope, still `https://example.com/v1/test1` redirects to `https://example.com/public/v1/` (and `https://example.com/v1/` returns 404). – Acidon Oct 19 '22 at 20:25
  • @Acidon, I have made slight changes in rules now, could you please try now, used different APACHE default variable, if this doesn't work then we will go step by step. – RavinderSingh13 Oct 19 '22 at 20:25
  • now I get 'Internal Server Error' on both `https://example.com/v1/test1` and `https://example.com/v1/` requests. – Acidon Oct 19 '22 at 20:29
  • @Acidon, fixed the typo now, kindly do check updated ones. – RavinderSingh13 Oct 19 '22 at 20:30
  • 1
    Now `https://example.com/v1/test1` redirects to `https://example.com/v1` (notice trailing slash missing) and gives 404, and `https://example.com/v1/` gives 404 as well. – Acidon Oct 19 '22 at 20:34
  • @Acidon, Ok taken care of `/` try edited ones now and let me know how it goes? – RavinderSingh13 Oct 19 '22 at 20:35
  • Redirect from `https://example.com/v1/test1` to `https://example.com/v1/` works now, but `https://example.com/v1/` still returns 404. – Acidon Oct 19 '22 at 20:39
  • @Acidon, ok do 1 thing your inner v1 .htaccess make it NULL OR remove it for time being and try only root htaccess with edited rules and let me know how it goes. I hope this should work, let me know how it goes. – RavinderSingh13 Oct 19 '22 at 20:40
  • I've removed `.htaccess` from `v1` folder, but still getting the 404s. Redirect works as expected. – Acidon Oct 19 '22 at 20:43
  • @Acidon, Ok, change only last line **to** `RewriteRule ^v1/?$ /v1/index.php [QSA,L]` once and let me know how it goes, make sure to clear your browser cache and then test your URLs please. – RavinderSingh13 Oct 19 '22 at 20:44
  • Now everything gives 404 and redirect is not working. – Acidon Oct 19 '22 at 20:46
  • @Acidon, ohhh :( but v1 folder has index.php present inside it right? And its htaccess is not there, can you please confirm these 2 points once? – RavinderSingh13 Oct 19 '22 at 20:47
  • Everything is correct. I also notice that I get 404 on any existing page inside the `v1` and no redirect. – Acidon Oct 19 '22 at 20:49
  • @Acidon, ok lets try another way, I have changed my rules again, kindly do check them once and let me know. – RavinderSingh13 Oct 19 '22 at 20:50
  • @Acidon, ohhh and also change your %DOCUMENT_ROOT to root(level before public) then it should work I believe, kindly try it out once. – RavinderSingh13 Oct 19 '22 at 20:52
  • Redirect loop on direct 'https://example.com/v1/' request, and on `https://example.com/v1/test1` request it redirects correctly but then goes into redirect loop too. – Acidon Oct 19 '22 at 20:53
  • @Acidon, yeah it means we are on right path. could you please try my updated rules once? – RavinderSingh13 Oct 19 '22 at 20:57
  • I've also changed DocumentRoot on server to remove the `/public` part and restarted the apache. Still getting the same redirect loops as in previous comment. – Acidon Oct 19 '22 at 20:58
  • @Acidon, request you to kindly try my edited rules as mentioned in previous comments and let me know if this helps. – RavinderSingh13 Oct 19 '22 at 20:59
  • I've applied your latest fix and redirect from `https://example.com/v1/test1` to `https://example.com/v1/` works correctly, but `https://example.com/v1/` still returns 404. – Acidon Oct 19 '22 at 21:01
  • @Acidon, considering that your %DOCUMENT_ROOT is path of root eg: `/root/hddocs` etc path just before 1 level of public and htaccess. And then change last line of current rules to `RewriteRule ^v1/?$ /v1/index.php [QSA,L]` and see how it goes? – RavinderSingh13 Oct 19 '22 at 21:02
  • Yes, %DOCUMENT_ROOT path is correct. I've changed last line, but still 404, redirect is working. – Acidon Oct 19 '22 at 21:06
  • @Acidon, ohhh I got it, I am sleep its 3 AM here. Try my edited rules once and Congrats me and us if we are Good here :) let me know quickly – RavinderSingh13 Oct 19 '22 at 21:07
  • @Acidon, Your welcome, I am very very very happy that I could help you, hard work paid off, cheers and keep asking Good questions on this great site, cheers, take care. Its 3 AM IST but I am Loving it :) – RavinderSingh13 Oct 19 '22 at 21:10