0
votes

I have a problem.

Have a custom mvc structure and everything works through RewriteRule in .htaccess while it is the root folder, but if I set it as subfolder it stops working.

.htaccess was

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
AddDefaultCharset UTF-8
Then I changed the base and added:
RewriteBase /mysubfolder/mySubSubfolder/

Cong file:
ServerAdmin [email protected]
DocumentRoot /var/www/rootfolder/
< Directory /var/www/rootfolder >
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
Order allow,deny
allow from all
< /Directory >
< Directory /var/www/rootfolder/sub/sub/ >
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
Order allow,deny
allow from all
< /Directory >

I tried some magic tricks with rewrite rule , but it seems i have a lack of knowledge. Would be great if u could help .

Thank you, sorry i'm a bit noob:)

1

1 Answers

0
votes

tldr;

Assuming the .htaccess file was not moved to the subdirectory with the MVC app...

Update the global redirect line:

RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

to:

RewriteRule ^(.*)$ subdir/index.php?route=$1 [L,QSA]

With most (if not all modern MVC frameworks in PHP), "all" non-file paths (URLs) are designed to be redirected to the "index.php" file. You might notice this line:

RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

It's designed to essentially take a regular URL, e.g. http://example.com/some/path and redirect it to http://example.com/index.php?route=some/path

The 2 lines that precede it state: "if the URL is not a request for a file"

RewriteCond %{REQUEST_FILENAME} !-f

"if the URL" is not a request for a directory"

RewriteCond %{REQUEST_FILENAME} !-d

then redirect anything matching the regex: ^(.*)$ and redirect it to the destination path

RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

I'm assuming you moved the project (but not the .htaccess file).

Assuming the above, if you moved your project into a subdirectory, you need to update the destination path so that the index.php path is correct.

e.g.

RewriteRule ^(.*)$ subdir/index.php?route=$1 [L,QSA]

--

To give a crashcourse breakdown of the regex:

^ indicates the string must start with the string provided, e.g. ^apple would mean the string would only match if it starts with the word apple

$ indicates the string must end with the string provided, e.g. banana$ would mean the string would only match if it ended with the word banana

. indicates any character

* following the "." means any number of characters (1 to infinity, theoretically)

So in short, ^(.*)$ means match pretty much everything!