(api) .htaccess rewriting for /backend and /frontend folders - .htaccess

I'm trying to host a nuxt + symfony project, and I'm kind of stuck here.
I suppose what I need is some .htaccess rewriting.
I have 2 separate folders :
/
- /backend, which is basically my symfony folder.
- /frontend, containing my nuxt app.
I would need to redirect all of the requests that do not contain "/api" to the /frontend folder, and all requests containing "/api" to "/backend/".
Example :
mydomain.com/articles/5 would redirect to mydomain.com/frontend/articles/5
mydomain.com/api/article/5 would redirect to mydomain.com/backend/article/5
Any help would be really useful,
thank you.

Assuming you have additional .htaccess files in the /frontend and /backend subdirectories that also use mod_rewrite to route the URL then you could do something like the following in the root .htaccess file:
RewriteEngine On
# Rewrite "/api/<something>` to backend
RewriteRule ^api/(.*) backend/$1 [L]
# Everything else rewritten to frontend
RewriteRule (.*) frontend/$1 [L]

Related

.htaccess need help to serve requests to index.php in non root folder

I have a specific website structure:
root:
styles.css
pages/index.html
folder_with_assets_1
folder_with_assets_2
folder_with_images
I've renamed index.html to index.php in order to get rid of .html extension in the URL
But the problem is that index.php is located not in a root folder, it's in pages folder.
Which right .htaccess rules could solve the problem in order to redirect requests to pages folder?
UPDATE
the screenshot with folder structure:
On the face of it, this just looks a standard front-controller pattern. Whether the front-controller is located inside a subdirectory or directly in the document root is largely irrelevant - the process is the same.
Assuming you are using the .htaccess file in the document root and there is no discernable pattern to the page URLs...
For example, using mod_dir FallbackResource:
FallbackResource /pages/index.php
Or, using mod_rewrite:
DirectoryIndex /pages/index.php
RewriteEngine On
RewriteRule (^|/)index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . pages/index.php [L]
UPDATE#1:
It's "onepage" website format with plenty of JS and CSS. There are only local URLs pointing to sections (href tag) and AJAX call to specific PHP files
In that case, it just looks like you need to change the DirectoryIndex - you don't need a front-controller pattern (as discussed above) at all.
For example:
DirectoryIndex /pages/index.php
Now, a request for the "homepage", ie the document root https://example.com/ will serve /pages/index.php.
UPDATE#2:
From your screenshot, it looks like directory listings (mod_autoindex) are enabled. These should be disabled at the top of the .htaccess file:
Options -Indexes
UPDATE#3:
From your screenshot, it would seem that what you have called "root" in your file structure is not actually your website's "document root", since you are accessing this location via a /test subdirectory, ie. localhost/test/. The directives above are assuming these files are located in the "document root", ie. localhost/ and there is no /test subdirectory. (Which I expect is how it is structured on your "live" environment?)
If your .htaccess file is located in the /test subdirectory and you are requesting localhost/test/ (as per your screenshot) then you will need to adjust this accordingly:
For example:
DirectoryIndex /test/pages/index.php
However, that will not work on the live site (assuming you don't have a /test subdirectory on live). Instead, you can simply omit the slash prefix, to make it relative.
For example:
DirectoryIndex pages/index.php
This should work OK in your case since you have a SPA (just a homepage URL).

Use .htaccess to redirect root to two different directories

I'm developing a website that has to co-exist with another one on an existing server. I know it's not an ideal situation but for timing reasons we don't really have any choice.
Request coming to the root of the URL have to be routed to the index.html of the new website, and everything else has to go the index.html of the old website (both of the websites are using react so every route goes to index.html).
I've tried to write a .htaccess doing this but with no success so far.
Here's an example of how it's supposed to work :
I have two folders, old/ and new/ each containing a website.
if a user goes to http://example/ -> it's serving content from the new/ folder.
if a user goes to http://example/test -> it's serving content from the old/ folder.
Below is what I've tried. It redirects / to the new/ folder, but rewrites the URL doing so. Furthermore, as soon as I try to redirect /* to the old/ folder things stop working.
# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect requests to index.php
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^$ new [L]
Try the following instead:
Options +FollowSymLinks
RewriteEngine On
# Rewrite the root only to new/index.html
RewriteRule ^$ new/index.html [L]
# Rewrite other URLs to old/index.html
RewriteRule . old/index.html [L]
You should rewrite directly to the file that is handling the request. If you rewrite to new only - without a trailing slash - then mod_dir will issue an external redirect to append the slash.
NB: This does assume that you have another .htaccess file in the /old/ subdirectory that contains mod_rewrite directives (a front-controller), otherwise everything (including static resources) will be rewritten to /old/index.html.
It redirects / to the new/ folder, but rewrites the URL doing so
I think you are using the terms "redirect" and "rewrite" the wrong way round. A "rewrite" is internal to the server, the URL does not change. This is what you are doing here... rewriting from root to /new/index.html etc.
However, a "redirect" is an external HTTP redirect that triggers a 3xx response and a second request to the server.

Symfony .htaccess

I configured a test folder for websites in hostgator, so far I have no problems in some flatform like wordpress,magento,joomla, I did not even change any seetings in .htaccess to make it run. Recently, I am working on a symfony2 project and upload my test application in the test folder I created. This is how I create a test folder
mywebsite.com/tests/wordpress_site(no problem)
mywebsite.com/tests/joomla_site(no problem)
mywebsite.com/tests/symfony2_site(403 forbidden error)
In my WAMP, I can accessed my symfony project
localhost/symfony_site/web/
I did not touch the .htaccess since this is just a test site in local machine.
How would you configure a symfony2 project in this setup?
mywebsite.com/tests/symfony_site
It is almost impossible to create clean Urls when installing symfony in subfolder...
With Apache, the better solution is to use a VirtualHost which have tests/symfony_site/web as DocumentRoot, and to use the built-in .htaccess file (which uses url rewriting module, or if it not available, uses a 302 redirection to app.php/)
But If you can't add a virtualHost to your server, it is hard...
An other way is to add a .htaccess at tests/symfony_site and put DirectoryIndex: web/app.php
(https://httpd.apache.org/docs/current/mod/mod_dir.html)
So you can have clean URLs... but be careful with this solution: the urls parsed by the router are relative to your site root (and not to symfony root...) so the router might be lost... (e.g if you have a root named /test you should rename it /tests/symfony_site/test... And it is the same for assets, you have to add the site directory, otherwise they will return a 404 error...
In my case, I just use existing .htaccess in web folder (or I create a new one with a DirectoryIndex Instead), so my app work with URLs like mywebsite.com/tests/symfony_site/web/
I hope this helped,
Vincent
You can try typing the following code into the .htaccess file to the symfony root folder
RewriteEngine On
RewriteBase /
RewriteCond $1 !^(img|files|images|favicon.ico|css|js|robots\.txt)
RewriteRule ^(.*)$ web/$1 [L]
RewriteCond %{HTTP_HOST} host.com
RewriteCond %{REQUEST_URI} !web/
RewriteRule ^(.*)$ web/$1 [L]

redirect /subdirectory to /symlink/subdirectory where /symlink points to /

i'm working on a drupal7 multisite setup based on subdirectories where example.com is used for the main website and example.com/subsite is another standalone drupal install. subsite is a symlink located in the root directory and also pointing to the root directory to give the subsite access to drupal core files.
now i have to make static content available via example.com/subsite/static, so i created a directory static in the root directory. that all works fine.
the problem is, that example.com/static is now also accessable and i want to prevent that.
i tried to redirect all requests to /static to /subsite/static resulting in inconsistent behaviour and redirect loops.
directory structure:
/
/{various drupal directories}
/subsite -> /
/static
rewrite rule:
RewriteRule ^/static/(.*)$ /subsite/static/$1 [R,L]
thx in advance
Is this what you want?
RewriteBase /
RewriteRule ^static/(.*)$ /subsite/static/$1 [L,R=301]
RewriteRule ^subsite/static/(.*)$ /static/$1 [L]
In the first line we redirect all call to /static/ to /subsite/static/.
In the second line we rewrite all call to /subsite/static/ to /static.
If it doesn't work please post your whole .htaccess file.
i fixed the redirect loops by using a rewrite condition, should have thought of that before.
RewriteCond %{REQUEST_URI} !^/subsite/(.*)$
RewriteRule ^static/(.*)$ /subsite/static/$1 [L,R=301]
thx anyway #florian-lemaitre for trying to help me

Redirecting to 404 error while accessing directory on web server

I want to redirect the user to 404 error file while they try to access directories in my server such as imgs, js, css.
One Way
I can put index.html to all those directories with 404 error.
Cons
I don't want to do this because their are alot of directories and sub-directories in my server.
Second Way
Create a 404.html file and redirect every directory url toward it.
How to acheive this this using .htaccess re-write rule?
Create a 404.html file and redirect every directory url toward it. How to acheive this this using .htaccess re-write rule?
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L,R=404]

Resources