use .htaccess to deal with requests for backend and frontend - php

First of all, I apologize for any mistake in English
well, my question is this:
I have a folder that has the backend, and receives requests in the following path
[root]/backend/www(which contains an index.php file that handles requests)
I have a folder that has the frontend, and receives requests in the following path
[root]/frontend/www(which contains an index.php file that handles requests)
I have also a folder named common that is shared between the backend and frontend (contains the libraries of the framework, among other files)
what in need, is when the access to this location:
h**p://localhost/andre/admin
rewrite it to:
h**p://localhost/andre/backend/www (how it currently is)
In other words, instead of accessing the backend by:
h**p://localhost/andre/backend/www
use this address:
h**p://localhost/andre/admin
all other requests that do not point to:
h**p://localhost/andre/admin
be accessed by:
h**p://localhost/andre/ (ie, point to
h**p://localhost/andre/frontend/www)
(I hope you can understand what I want)
I need help, on a way to do this with .htaccess file, I can not use Symlinks or Subdomains for this problem...
I've searched a lot in Google and here, but found nothing that worked or who could help me.
PS: I am using Yii Framework with YiiBoilerplate, but I think the solution should not be out there
Thanks in advance!

Solved, with this code:
RewriteEngine On
RewriteBase /andre/
RewriteRule ^admin(/.*)?$ backend/www/index.php/$1 [L,NC]
RewriteCond %{REQUEST_URI} !/(frontend|backend)/ [NC]
RewriteRule ^(.*)?$ frontend/www/index.php/$1 [L,NC]

Put this code in your /andre/.htaccess file:
RewriteEngine On
RewriteBase /andre/
RewriteRule ^admin(/.*)?$ backend/www$1 [NC,L]
RewriteCond %{REQUEST_URI} !/(frontend|backend)/ [NC]
RewriteRule ^(.*)$ frontend/www/index.php/$1 [L]

Related

Rewrite/hiding some URL in .htaccess

I'm trying to get right syntax for .htaccess without any result...
I've a URL structured as domain.com/app/public/pageName .
It's working fine but I would "hide" the 'app/public/' part in browsers, basically doing something like:
[real URL] domain.com/app/public/pageName -> domain.com/pageName [what users type and see in browsers]
I think in that way it should be more readable and seo-friendly.
As I understood from docs (and maybe it's wrong because it's not working...) I should tell to Apache to map/redirect all URL like domain.com/pageName to domain.com/app/public/pageName , but only internally, in order to show the minimal URL in users' browsers.
Right now I have something like:
RewriteEngine on
#RewriteBase /app/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ https://localhost/app/public/index.php?url=$1 [QSA,L]
(I'm using full URL with https://... in order to get something that will be quick and easy to adapt when I upload all to my hosting, is it right?).
Problem is that RewriteRule actually change the URL, because it perform a redirect and URL rewrite it's not handle internally.
So, first of all: is it possible what I'm trying to do? If so, how can I handle the URL rewrite only internally?
Everything should be uploaded to a shared hosting, so I don't have other than .htaccess.
Anyway, I can consider to upgrade to a vps if there are not other possibilities...
Thanks!
==============
EDIT (should be more clear now)
tl;dr version:
I'm looking for a method that let users to type domain.com/pageName (and they will see that address in their browsers) and rewrite internally that URL in order to point to domain.com/app/public/pageName.
==============
More: after /app/public/ there can be an arbitrary number of elements, separated by / . All of these elements are appended at the end of the URL after index.php. At the end URL looks like:
domain/app/public/index.php?url=lot/of/elements/here
This is already working with the RewriteRule posted above, I would keep that too.
Thanks!
This is working fine for me, Hope it will work for you as well.
Check .htaccess here
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/app/public
RewriteRule ^app/public/(.*)$ /$1 [L,QSA]
Just for reference, I found a solution, maybe will be usefull for someone.
Basically I moved .htaccess to the root server (instead of /app/public directory) and changed the RewriteRule as follow:
RewriteEngine on
RewriteBase /app/public/
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT]
Now it's working (at least on localhost).
What do you think? Are there any side effects with this config?

htaccess for domain and subdomain with laravel

I'm trying to figure out how to set up a domain and subdomain to work on a shared hosting account. It is a Laravel 5.1 application.
My access file is
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^ index.php [L]
I just purchased another domain and added it on, but I get a 500 error. I renamed the access file and then it worked. So it has something to do with the access file. Essentially I want two separate domains with and I'm wanting two separate laravel applications, one for each.
I'm not familiar with atacceess.
Maybe, you get a redirect loop, because the rule isn't protected by a condition. Although the default htaccess of Laravel should already contain them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Olaf Dietsche's answer does work for me.
Here is another thing I came upon a website that also worked just before I saw his post. I guess I was reading that this would send a 404 to that directory.
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?main-topdomain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain-folder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
So along with this comes another question if anyone is in my boat. I have my subdirectory inside of my root directory.***
dlaugh.com/public_html
laravel folders and access**
inluding
app
bootstrap
config
database
etc...
but also I have my sub folder
app
bootstrap
config
database
etc...
**mysubdomain in that folder**
Is it better practice to put
-main_domain_folder and
-subdomain_folder
in public_html
and then the
/app
/config
/database
would be in the main_domain_folder rather than passing the subdomain through the main domain?

How to redirect all API requests using .htaccess, while keeping asset requests intact?

TL; DR: I would like to hit the index-api.php file if api is found in the URL, but then simply keep all other requests pointing to the site/dist directory as if it were the 'root' of the site.
So, I've spent way too many hours on this and trust me, I've dug through all of the resources for mod_rewrite. I guess I'm just not quite understanding and figured I'd ask on here.
What I want to do, in theory, seems simple. I'm building a single page application (Angular App) using Grunt, outputting that to a the root of a WordPress install. The WordPress install is simply serving up an API using the WordPress JSON API plugin, so I want the root of the site to hit my Grunt directory (located at site/dist/index.html), but all requests to siteurl.com/api to hit the index.php file and proceed normally.
Keep in mind I have other assets / images located in this site/dist directory, so ideally, it would be awesome if all requests to the site root would simply use this folder as the "base" of the site (e.g. a request to siteurl.com/images/testimage.jpg pulls from site/dist/images/testimage.jpg).
I feel like I'm onto something here and am surprised I couldn't find anything that directly tackles this issue.
What I've done now is renamed the index.php from WordPress to index-api.php and left it the same:
index-api.php:
<?php
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wordpress/wp-blog-header.php');
// phpInfo();
.htaccess:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ index-api.php [L]
RewriteRule (.*)$ site/dist/index.html [L]
</ifModule>
I tried a myriad of other efforts from a few posts trying to get this working, and it seems to me like it should work fine. The funny thing is, if I comment out the last line RewriteRule (.*)$ site/dist/index.html [L] the api request works normally as expected, so I know I'm close.
Any suggestions?
Would appreciate anyone's help on this, it's been really confusing!
In the first place you'll need to make sure that requests made to /index-api.php are not matched and rewritten by the second rule. In the second rule you can use $1. $1 will be replaced with whatever was matched in the first capture group. We'll also need to make sure that the second rule will not match what it rewrites, or we'll end up with an infinite loop and an internal error.
You can use the $1 in the first rule too, as I show below:
RewriteRule ^api/(.*)$ index-api.php?url=$1 [L]
RewriteCond %{REQUEST_URI} !^/site/dist/
RewriteCond %{REQUEST_URI} !^/index-api\.php
RewriteRule (.*)$ site/dist/$1 [L]
I recommend reading the documentation of mod_rewrite to get a better understanding how you can use it and what things you have at your disposal while rewriting url's.

PHP MVC & .htaccess 301 Redirect

I am restructuring a website to a MVC framework and am in the process of moving everything from root the root directory into an organized file structure. So now instead of going to domian.org/homeloans.php, the users will need to do domain.org/loans/homeloans. Here is the .htaccess file I use to direct website traffic.
RewriteEngine on
DirectorySlash on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
Shouldn't this be as simple as adding:
Redirect 301 /homeloans.php https://domain.org/loans/homeloans
after turning the rewrite engine on? However, when I do this, I get this in my address bar along with a 404: https://domain.org/loans/homeloans?rt=homeloans.php
Please advise.
The problem seems to be not with the rewrite, but with the ability of your new MVC site to handle this URL. It looks like it can handle:
/loans/homeloans
but not
/loans/homeloans?rt=homeloans.php
Try accessing both directly; if the latter doesn't work but the former doesn't, then you know the issue is with the routing configuration of your MVC application.

.htaccess file not working with certain servers

Hello and thank you for taking the time to help me solve my problem. I like to build all of my web projects using a simple MVC Framework I have created along my travels. I seem to be having a problem getting the .htaccess file to work correctly on specific servers.
For example...
If you go to:
http://thomsonbrothersindustries.com/northside/ and attempt to access a page (right now only "About Us" is functional - http://thomsonbrothersindustries.com/northside/about) but as you can see the "about" page just directs you to a 404 Error.
Now, if you go to: (the same site, but on a different server)
http://ericzdisposal.com/northside/
http://ericzdisposal.com/northside/about
everything works fine...
Here is the .HTACCESS file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
My privileges are limited on the server that is currently not working and I'm still working on getting better access to domain tools, but in the meantime I'm just trying to get a better idea of the problem and hoping there might be an easy fix in my future.
In addition to what #bigman suggested, make sure that AllowOverride directive setting is not very restrictive for your website directory. For mod_rewrite to work in .htaccess, you need to set AllowOverride FileInfo, but for testing purposes, you can set AllowOverride All.
Looks like adding the directory into the .htaccess file did the trick!
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ **/northside/**index.php?rt=$1 [L,QSA]
I didn't realize this was important because on the other server it did not matter
Thanks all!

Categories