How to route specific URL segments to separate web root directories? - apache

My yii project has following structure:
webapp
----frontend
--------www
------------index.php
----backend
--------www
------------index.php
----api
--------www
------------index.php
https://github.com/tonydspaniard/yiinitializr-advanced
Apache 2.2
Each www directory has own .htaccess file. For example "webapp/frontend/www/.htaccess":
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/].*)$ /index.php/$1 [L]
For separate parts of application i use subdomains "api.webapp.com","admin.webapp.com" (symlinks in public_html folder):
webapp.com -> webapp/frontend/www
api.webapp.com -> webapp/api/www
admin.webapp.com -> webapp/backend/www
But i have problems with cross domain ajax requests to api, and i want to get something like this:
http://webapp.com -> webapp/frontend/www/index.php
http://webapp.com/api/ -> webapp/api/www/index.php
http://webapp.com/admin/ -> webapp/backend/www/index.php
What is the right way to route URL segment to specific web root directory? Aliases, .htaccess, symlinks, maybe something else?

As aCodeSmith said, you can properly configure .htaccess file to solve the issue.
The document root of http://webapp.com should be webapp.
Right in the document root you should create .htaccess file with route rules like that.
RewriteCond $1 ^api\/
RewriteRule ^(.*) api/www/index.php [L,PT]
RewriteCond $1 ^admin\/
RewriteRule ^(.*) admin/www/index.php [L,PT]
RewriteRule . frontend/www/index.php
Hope, it will help.

Thanks, guys.
Now i have two working solutions:
1) by Alex Akr
webapp is root
webapp/.htaccess:
RewriteCond $1 ^api\/
RewriteRule ^(.*) api/www/index.php [L,PT]
RewriteCond $1 ^admin\/
RewriteRule ^(.*) admin/www/index.php [L,PT]
RewriteRule . frontend/www/index.php
2) webapp/frontend/www is root.
In apache config set aliases:
Alias /api /var/www/webapp/data/www/webapp/api/www
<Directory /var/www/webapp/data/www/webapp/api/www>
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
In /webapp/api/www/.htaccess
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule ^([^/].*)$ /api/index.php/$1 [L]

Related

Need to remove a specific folder from .htaccess mod-rewrite redirect rules

I'm using some software that sits in the /var/www/html folder and manages URL redirects through .htaccess (the software isn't Wordpress, but it manages URL redirects in a similar way).
I need to carve two folders (/var/www/html/folder1 and /var/www/html/folder2) out from the redirect rules, which are at the very bottom of the .htaccess file. I read through a ton of documentation but can't seem to make the following work in .htaccess (which sits in /var/www/html):
Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
#force HTTPS
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#carve out these folders
RewriteRule ^/folder1($|/) - [L]
RewriteRule ^/folder2($|/) - [L]
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
When I go to the directories in question (eg. https://example.com/folder1), I get: Forbidden You don't have permission to access /folder1/ on this server.
Any ideas?
I can't comment as I'm new but do the two folders have any sort of index page? Otherwise you will get a 403 response because you have disabled indexes at the top of your file excerpt.

Rewrite existing file request to subdirectory

I have a php framework on a shared hosting and this is my directory structure:
root (can't change because of shared hosting; here's the .htaccess)
vendor
web (index.php, assets in subfolders like css, images or js)
My .htaccess looks like this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ web/index.php [L]
</IfModule>
Now the framework routing works, but I want Apache to search all existing files (${REQUEST_FILENAME} !-f) only in the web folder, not from the root level.
Example:
http://www.domain.tld/hello/world (virtual path) is rewritten to index.php
http://www.domain.tld/css/bootstrap.css is not found, because the file is in the web/css folder.
http://www.domain.tld/web/css/bootstrap.css would match, because of the RewriteCond %{REQUEST_FILENAME} !-f
Is it possible to combine RewriteCond %{REQUEST_FILENAME} !-f with a folder rewrite for existing files only? All other request must be rewritten to the index.php
All in all I want it similar as setting the VirtualHost->DocumentRoot to the web folder.
Thanks!
You can use these rules in site root .htaccess:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ web/index.php [L]
# if file is found in web/ folder then route it
RewriteCond %{DOCUMENT_ROOT}/web/$1 -f
RewriteRule ^(.+)$ web/$1 [L]
# otherwise route it to web/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ web/index.php [L]
</IfModule>

Mod rewrite to remove subdirectory and file extension without breaking DirectoryIndex

I have all site pages in a subdirectory like this...
http://www.example.com/pages/myfile.php
I want the URL to look like this...
http://www.example.com/myfile
Where both the subdirectory called pages and the .php file extension are removed from the URL.
My latest (partial) attempt...
Options All -Indexes +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/pages%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/pages%{REQUEST_URI} -d
RewriteRule ^(.*)$ /pages/$1.php [NC,L]
However, this totally breaks DirectoryIndex. When I go to http://www.example.com/ or http://www.example.com/foo/, I get a 404 error instead of defaulting to index.php as defined by DirectoryIndex.
Apparently, it treats everything as a file name instead of recognizing the lack of a file name (directory) and attempting to use index.php.
I tried incorporating this solution into mine, it fixed the DirectoryIndex issue, but it broke everything else.
Is there a solution? Please include a detailed explanation within your answer so I can learn where/how I was going wrong.
Try this in root .htaccess:
Options All -Indexes +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# add a trailing slash if pages/<uri> is a directory
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteRule ^/?$ pages/index.php [L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# if corresponding .php file exists in pages/ directory
RewriteCond %{DOCUMENT_ROOT}/pages/$1\.php -f
RewriteRule ^(.+?)/?$ pages/$1.php [L]
# route all requests to pages/
RewriteRule ^((?!pages/).*)$ pages/$1 [L,NC]

.htaccess rewrite rule with DocumentRoot subdomain

I'm trying to implement some htaccess rewrite rules. However in not a pro in htaccess.
I have a subdomain gateway in (as described in: .htaccess and rewrite sub domains)
Now i would like to use SEO friendly URL's. The gateway has some subfolders i.e. email in this case.
http://gateway.example.com/email/Param1/Param2/
This url should be rewritten to http://gateway.example.com/email/index.php?hash=Param1&hash2=Param2.
In the email folder I have created a .htaccess file with the following contents:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
RewriteRule ^/(.*)/(.*)/ index.php?hash=$1&hash2=$2
When requesting the http://gateway.example.com/email/Param1/Param2/ i'm now getting a 404. What is wrong with the rule?
You should do it this way
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^/]*)/([^/]*)/ /email/index.php?hash=$1&hash2=$2 [L]
you can try this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/([^/]*)/([^/]*)/ index.php?hash=$1&hash2=$2
*you need to configure apache(httpd) to accept UrlRewrite by changing AllowOverride from non to all like this :
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>

How can I remove "index.php" from my links?

Normally I access a page like this: http://localhost/codeigniter/index.php/auth/login
However I want to be able to access to the page like this : http://localhost/codeigniter/auth/login
I have no information about http.conf and .htaccess configurations.
I have set $config['index_page'] = ''; in codeigniter/application/config/config.php
my http.conf file is at its default except uncommentnig "LoadModule rewrite_module modules/mod_rewrite.so"
I inspected many proposed .htaccess files but I could not understand the logic behind the rewriting rules.
Here is the most successful .htaccess content for me. It is located at /www/CodeIgniter/ .
(by the most successful I mean that this is the only one that gives 404 error created by the CodeIgniter not by the server.)
RewriteOptions Inherit
Options -Indexes
Options +FollowSymLinks
RewriteBase /CodeIgniter/
# Set the default file for indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
# activate URL rewriting
RewriteEngine on
# do not rewrite links to the documentation, assets and public files
RewriteCond $1 !^(images|assets|uploads|captcha)
# do not rewrite for php files in the document root, robots.txt or the maintenance page
RewriteCond $1 !^([^\..]+\.php|robots\.txt)
# but rewrite everything else
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 index.php
</IfModule>
I hate requesting direct solutions however unfortunately, it is my last resort :/
Can you give me a right solution to my problem?
It's my repeat answer:
Codeigniter issues with paths in localhost (XAMPP)
please create .htaccess file in project folder and write:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
You don't need to define in base_url in config file:
$config['base_url'] = ''; // blank it.
put this on your htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
make sure the rewrite module is enabled on your server
This is the simplest one i found which works for me :-
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]