I have a small mvc application where I'm trying to force the url to go from www.example.com to www.example.com/public.
In order to do this, I have a .htaccess file in the root (/) and then another .htaccess within the /public folder to point to index.php with friendly urls. I believe the issue is within the htaccess file that sits in the public folder, I need all requests to go through index.php
I have tried a few threads on stackoverflow but seems to give me a server 500 error when I access www.example.com
.htaccess of root
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /public/
</IfModule>
.htaccess of public folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteCond %{REQUEST_FILENAME}% !-d
#allow images, css and js links
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) /public/index.php
Related
I'm basically trying to have everything that's not a file (.css, .js, .jpg, etc...) rewrite to index.php to create sym-links. The code I have in my .htaccess file works for my root directory and for a single subdirectory So. "localhost/" and "localhost/help" both work, but "localhost/help/article-27" does not work. The .htaccess seems to rewrite everything when I try to use that second sub-directory, including JS and CSS files. Does anyone know why that is? Here's my code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
</IfModule>
As darren mentioned as per the comments it's not required to have $1 !!.(gif|jpe?g|png)$ [NC]
This will work for all the levels and CSS files whether symlink / direct links.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
</IfModule>
I am trying to configure .htaccess for my web site.
there are two folders in my web site 1 - app , 2 - public.
i want to load index.php from public folder and restrict all direct access to app folder
the .htaccess i am using returns result 403 Forbidden, but if i move index.php in root directory it is working, i just want to point that index.php is in public directory and send all requests there.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /public/
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Restrict php files direct access
RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.php[?\ ]
RewriteRule \.php$ - [F]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
Well i solved this problem like this,
for root directory i managed .htaccess like the code given down, the idea is to redirect all calls to public dir.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
And for filtering requests i used to put second .htaccess to public dir
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Restrict php files direct access
RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.php[?\ ]
RewriteRule \.php$ - [F]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
I am using this htaccess for my application.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any URL I will type in my browser, if the respective file doesn't exist, it will open the index.php.
The problem is I have many applications like this installed on the same domain.
For example, the root contains an index.php file and a htaccess like above.
I have also, /store, which contains a index.php file and a htaccess like above, too, but when I access /store/something it opens the index.php of root (not the correct /store/index.php
How can I solve this problem (how to make the htaccess of root to not override the htaccess of /store)? Thanks!
You can have a skip directory rule to skip all the directories:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# skip index.php and some directories
RewriteRule ^(store/|site1/|site2/|index\.php$) - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This is confusing to explain, but I am rewriting urls for a router/framework I am making. I am directing my root to a folder named public and in that folder I handle the rewriting for index.php and such. But I want to be able to access css and a js folder. Anyways, whenever I go to /css it will take me to /public/css and lets me view files and lists directory but when I view a file it lets me view the file without changing the URL. So basically, if someone goes to /css I want it to stay like that, and not be changed to /public/css.
root .htaccess file
RewriteEngine On
RewriteBase /
RewriteRule ^$ /public/ [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1
public .htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Change your root .htaccess to this:
RewriteEngine On
RewriteBase /
RewriteRule ^$ public/ [L]
RewriteCond %{REQUEST_URI} !^/(public|css|js)/ [NC]
RewriteRule ^(.*)$ public/$1 [L]
Now this rewrite rule won't affect /css/ and /js/ folders.
I am new to url re-writng and is currently facing problems with rewriting url for multiple applications in codeigniter. I have scanned the whole stackoverflow with some potential answers but it is still not solving my problems entirely
My directory at the root is as such
-application
--administrator
--frontend
What I am looking for is for my users to access my web site (frontend) with http://www.mydomain.com/ And my admin (backend) as http://www.mydomain.com/admin/
my current .htaccess is as such
<IfModule mod_rewrite.c>
RewriteEngine On
# If the user types just "admin".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin$ administrator.php [L,QSA]
# If the user enter in any admin section, like "admin/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin\/(.*)$ administrator\.php/$1 [L,QSA]
# If the user types any site section, like "site/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index\.php/$1 [L,QSA]
ErrorDocument 404 /index.php
</IfModule>
code from CodeIgniter multi-application .htaccess problem
I can get what I desire at the front end with the above .htaccess, but I can't get it working on my back end. Basically, www.domain.com/admin gives a 404 page not found error.
Please advice me on the problem. Also, please guide me on where I should place this .htaccess file? at the root? inside the application folder? or inside the frontend and administrator folder respectively.
Many thanks.
Regards.
Why not something like this in your root directory.
RewriteEngine on
RewriteCond $1 !^(index\.php|update\.php|assets|admin|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
This will basically ignore any directories such as "admin" to route to the frontend application. You will have to set your config.php and remove index.php from the controller.
Found a way to work with this problem already. It is not a very smart way of doing it but it at least worked.
First, I created an 'admin' folder in the root, with a copy of the application's index.php
Which effectively make my directory looking like this.
-admin
--.htaccess
--index.php
-application
--frontend
--administrator
-index.php
-.htaccess
For this solution you will need to have two .htaccess files in different location.
For the .htaccess at the root, it should look like this.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|update\.php|administrator\.php|assets|admin|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
For the .htaccess in the admin folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
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>
While this works on localhost, it is noteworthy that it may not work on rented servers because the file system in your host may be different.