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]
Related
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
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]
I want to remove upload folder from opencart URL. I have tried with following code
RewriteEngine on
RewriteBase /
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Route all request to production folder
#RewriteRule ^(.*)$ upload/$1[L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?siteName.ae$
RewriteCond %{REQUEST_URI} !^upload/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ upload/$1
RewriteCond %{HTTP_HOST} ^(www.)?siteName.ae$
RewriteRule ^(/)?$ upload/ [L]
But when I adding this code then login does not working.
Try this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/upload/
RewriteRule ^(.*)$ /upload/$1 [L]
You need to change the rewrite base to /{foldername here}/
I have 2 domains and 2 servers example.com and example2.com. for eg: example.com is my main domain. i upload my site images into example2.com
my current image path http://example.com/data/photos/sample.jpg.
but my all images are in http://example2.com/data/photos/sample.jpg
So i create htaccess 301 redirect
RewriteEngine on
RewriteBase /
RewriteRule ^data/photos/(.*)$ http://example2.com/data/photos/$1 [R=301]
But its not working for me. Because of i called full URL into my all pages. so any one suggestion its very helpful for me
Updated my old Htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
RewriteEngine On
RewriteBase /
RewriteRule ^data/photos/(.*)$ http://example2/data/photos/$1 [R=301]
</IfModule>
You need to reorder your rules. Your final htaccess should look something like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(data/photos/.*)$ http://example2.com/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
change it with this
RedirectMatch 301 ^/data/photo/ http://example2.com/data/photo/
to create a 301 for the entire directory.
I'm trying to force www on my website using a RewriteRule in the htaccess file.
I'm using FuelPHP and I have to have the following rewrite rule in the htaccess file but the methods I found to force www didn't play nice with my current setup.
Current htaccess code
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Code that I found here to force www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
How can I combine these to enable forcing www and still have the other rule in the htacess file?
Try using this:
RewriteEngine on
RewriteBase /
# Redirect to wwww
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
FuelPHP comes with a .htaccess file in the public folder that contains a rewrite example for this situation.