How do I remove /public from the URL? - php

I am using Slim framework to build a website. How to I re-write the url using .htaccess ?
This is what my .htaccess file looks like, any route is redirected to index page, I got this from the Slim Documentation
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
This is my directory structure.
|--app
|--bootstrap
|--public
|--css
|--js
|--index.php
|--.htaccess
|--routes
|--vendor
|--views
|--composer.json
|--composer.lock
This how it works now
http://localhost/SlimApplication/public/ - points to the index page
http://localhost/SlimApplication/public/Users - points to the users page
This is how I want it to work
http://localhost/SlimApplication/ - should point to the index page
http://localhost/SlimApplication/Users - should point to the users page
What can I do to make this work ?? I am new at .htaccess and Slim as well so any help would be great. Thanks, Cheers !

Create new .htaccess file in level up folder hierarchy (the same as public folder). RewriteBase /public/ directive is what you need.
RewriteEngine On
RewriteBase /public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Related

.htaccess for routing react and php files

I have a react app whoe entry point is index.html And I have another product.php file on the same directory.
For all routes I want to point to index.html except routes like domain.com/project/product/t/100
My app is acceible from domain.com/project/ a I have hosted it in project folder.
For the url /project/product/t/100 I want to point this to products.php
NOTE : I have tried 100s of combination from stackoverflow and other site so please post only tet answers
HINT : It would be ok if I can just exclude /project/products/ path
This works but css, js files also points towards inde.html file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^products/(.*)$ products.php [NC,L]
RewriteRule ^(.*)$ index.html [L]
</IfModule>

Remove Subfolder[Codeigniter] only for specified URLs using Htaccess

My Folder Structure
Root Domain
assets/ (contains css, images, js folders)
subfolder/ (contains Codeigniter Project inside and seperate .htaccess)
index.php
contact.php
.htaccess
Root Htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)subfolder
RewriteRule ^(.*)$ subfolder/$1 [L]
Subfolder(Codeigniter Project) Htaccess
DirectoryIndex index.php index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|install|update)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For godady Shared Hosting Server uncomment the line below
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
List of Subfolder URLs
http://example.com/subfolder/products/
http://example.com/subfolder/product/product-slug-123
List of Root URLs
http://example.com/index.php
http://example.com/contact.php
In my Codeigniter Project I've set Products controller as default_controller.
So the problem is, when I try to access the index.php page in the Root folder it only displays the Codeigniter products list as Home page.
I need following URLs:
http://example.com/index.php
http://example.com/contact.php
http://example.com/products/
http://example.com/product/product-slug-123
Any solutions with htaccess or with code?
Finally I got this working.
RewriteCond %{REQUEST_URI} !((.*)subfolder|^/assets) // to Exclude this
RewriteCond %{REQUEST_URI} (^/products(.*)|^/product(.*)) // to Include this
RewriteRule ^(.*)$ subfolder/$1 [L]
Exclamation(!) symbol used to 'not' matching urls.
So I removed Exclamation symbol and used another RewriteCond (Condition) to redirect only if the url contains segment as products or product http://example.com/products/ or http://example.com/product/product-slug-123. It's working perfectly now. I thought, sharing this would be useful to someone. Happy coding.

How to hide path to real index.php

I have web project where index file is on deep folder.
Lets say my project index is on localhost/xxx/a/b/c/index.php, but I want to hide /a/b/c path become localhost/index.php.
How to code .htaccess and where I should put it?
Thank you.
Try the following in public_html/.htaccess
RewriteEngine on
#if the request is not for and existent file
RewriteCond %{REQUEST_FILENAME} !-f
#and the request is not for and existent directory
RewriteCond %{REQUEST_FILENAME} !-d
#then rewrite it to /a/b/c/request
RewriteRule ^(?:xxx/)?(.*)$ /xxx/a/b/c/$1 [NC,L]
This will internally redirect
example.com/file.php
to
example.com/a/b/c/file.php

.htacces redirect assets to assets folder

I have my PHP site setup with a folder for each site (e.g Login), which has an Index.php file and site-specfic assets. However, some of the assets, which are required by every single page, are stored in a 'Assets' folder located directly under the highest level (does that make sense?).
I have played around with .htaccess and got this code
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://janberktold.com/Assets/$1 [R=301,L]
However, my problem is: It redirects
localhost/Login/test.css
to
localhost/Assets/Login/test.css
instead of
localhost/Assets/test.css
How do I get my server to redirect to the correct path?
Try this
RewriteEngine on
RewriteBase /
# Rewrite if the file does not exists
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite only if the URI does not starts with Assets
RewriteCond %{REQUEST_URI} !^/Assets
# Rewrite any assets file
RewriteRule ([^/]*).(css|js|png|jpe?g)$ Assets/$1.$2 [L]
This should rewrite any assets files localhost/dir/file.css or localhost/dir/dir2/file.css to localhost/Assets/file.css
Replace your rules with:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:[^/]*/)*([^/.]+\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /Assets/$1 [R=301,L,NC]

Is it good practice to make system root unable to be accessed?

Let's say we have the following schema:
root/
application/
-public/
index.php
css/
img/
javascript/
...
system/
...
I was thinking that if I create an .htaccess file in the root with the following rules:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/public/index.php?url=$1 [QSA,L]
I would be able to make other folder except public unable to be accessed. Assume index.php is the file that load all the file the application need and include and require them (with relative paths obliviously): the application is actually run in index.php and every public file such as css stylesheet, images or javascript script are in the public folder. So you, as user, cannot access the system folder because if you digit www.site.com/ it refers to root/application/public/. Am I right?
But the htaccess rule I set there does not work... If I put that htaccess in the root folder and I try to access the root folder the browser shows me the list of the file of the root folder (application and system). Why isn't it working?
I used this while ago when index.php was in the root folder:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
and it worked just fine. Why isn't the new one working?
The document root of you project should be set to the /public folder.
This way that's the only folder to web-user would have access to.
Edit: for more info check the apache manual http://httpd.apache.org/docs/2.0/mod/core.html#documentroot
My initial code was right. Except for the third line which obliviously considered / a folder and made me see it instead of redirecting to application/public/.
Commenting the third line everything worked fine.
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/public/index.php?url=$1 [QSA,L]

Categories