Create exceptions in htaccess - php

So I was trying to load this project using .htaccess to redirect to webroot
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ Webroot/ [L]
RewriteRule (.*) Webroot/$1 [L]
</IfModule>
Now in that webroot folder I want everything to load on the index.php which required another htaccess in the web rooot folder
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [PT,L]
</IfModule>
The question is I want to make an exception that if the route is {url}/public, it can/should load files or images from there.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.html [L]
Change sub-directory to your directory

Related

Rewrite url with subdirectory

I have got domain main project for this domain in web directory. Now I need to place similar project in subdirectory of domain's root.
domain.com
|-web
| |-index.php
| |-assets
|
|-subdirectory
| |-web
| |index.php
| |-assets
My example url for first project domain.com/homepage is rewriting to domain.com/web/index.php?url=homepage using htaccess. Assets url is rewrites to assets directory.
/.htaccess
RewriteEngine On
RewriteRule ^(.*)$ web/$1 [L]
/web/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(gif|png|jpe?g|ico|swf|js|css)$ [NC]
RewriteRule .* - [R=404,L]
# rewrite assets version to orginal location
RewriteCond %{REQUEST_URI} assets/([0-9]+)/
RewriteRule ^assets/([0-9]+)/(.*)$ assets/$2
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]
It works well but how to extend this htaccess or add another .htaccess in subdirectory to rewriting subdirectory url?
Example domain.com/subdirectory/homepage sholud be rewrite to domain.com/subdirectory/web/index.php?url=homepage
SOLUTION:
/.htaccess and /subdirectory/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ web/$1 [L,QSA]
</IfModule>
/web/.htaccess and /subdirectory/web/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(gif|png|jpe?g|ico|swf|js|css)$ [NC]
RewriteRule .* - [R=404,L]
# rewrite assets version to orginal location
RewriteCond %{REQUEST_URI} assets/([0-9]+)/
RewriteRule ^assets/([0-9]+)/(.*)$ assets/$2
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>
Finally:
domain.com/start rewrites to domain.com/web/index?url=start
domain.com/subdirectory/start rewrites to domain.com/subdirectory/web/index?url=start
Exclude the /subdirectory rewrite on root directory, and apply htaccess rules under the folder subdirectory same as root directory.
/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/subdirectory
RewriteRule ^(.*)$ web/$1 [L]

CodeIgniter 404 Page Not Found htaccess

I got 404 Page Not Found when I tried to access my file without the index.php.
this is my .htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /HR/
# If your project is in server root then should be: RewriteBase/
# If project is in folder then it should be: RewriteBase /folder_name/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^.(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I have been using this one
Place this in main directory out side of application folder.
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Also make sure if your using codeigniter 3 that the first letter of class and file name must be uppercase only.
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Note: .htaccess vary depending on server. In some server (e.g.: Godaddy) need to use the following .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

.htaccess multiple public folders

My application is quite large and it is split into areas and I would like to have a seperate public folder in each of these areas for images and scripts that are only needed within that specific area.
I am currently directing all traffic to my public folder with the following:
RewriteEngine On
RewriteBase /
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
And in the public folder I have the following .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Is it possible to allow requests to:
http://example.com/area/public/scripts/myscript.js
Change your root .htaccess to this:
RewriteEngine On
RewriteBase /
RewriteRule ^$ public/ [L]
RewriteRule ^((?!area/public/).*)$ public/$1 [L,NC]

.htaccess returns 500 for a simple redirect in Laravel

I have created an app in Laravel.
My initial .htaccess was:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And it worked properly for the domain api.viglug.org.
After I added the m.forum.viglug.org domain (with the same root directory of api.viglug.org).
At this point was exactly the same to call api.viglug.org and m.forum.viglug.org.
I wanted to use the m.forum.viglug.org for the folder api.viglug.org/mobile so I thought it would have worked with this .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} m\. [NC]
RewriteRule ^(.*)$ index.php/mobile/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I was wrong. It keeps returning error 500 if I use m.forum.viglug.org, while api.viglug.org works as expected.
How can I fix this?
The original rewrite only runs on files which do not exist as real files or directories, due to these two conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Have you tried adding those two conditions above your additional rule?
# Mobile site
RewriteCond %{HTTP_HOST} m\. [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/mobile/$1 [L]
# Normal site
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

VHost & htaccess - 500 Internal Server error

I use ISPConfig 3 to manage my domains/subdomains.
I added subdomain parim.kristian.ee which would redirect to web/parim/ (note: web/ is my document root).
ISPConfig generated such redirect to apache config:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^parim.kristian.ee [NC]
RewriteRule ^/(.*)$ /parim/$1 [L]
Now if i'll try to get static resources, such as http://parim.kristian.ee/images/1x1.gif, it serves fine, but when redirect is to codeigniter, it doesn't work.
Htaccess in web/parim/ looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
Options -Indexes
#Handle CodeIgniter request
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 ./index.php
</IfModule>
NOTE: accessing the same folder via http://kristian.ee/parim/ works!
I s*ck at htaccess, so any help is appreciated.
# To remove index.php from URL
RewriteEngine On
RewriteBase /parim
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Or Simple remove RewriteBase /parim

Categories