.htaccess for routing react and php files - php

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>

Related

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 do I remove /public from the URL?

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]

Need Help To Protect my MVC PHP app folder using htaccess

I have an issue with my .htaccess.
Let's say that my app folder are:
/APP/
/CONTENT/
/UPLOADS/
/ASSETS/
And I have a .htaccess file that point all requests to my index.php, and the index php there is an included file called router.php that manage my roots:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\s\S]*)$ index.php?url=$0 [QSA,L]
What i would want to do right now is to point APP and CONTENT folder also to the .htaccess and like that, i can display my custom 404 error page.

mod-rewrite RewriteBase not working correctly from root

Hi I have been following the "Build a PHP MVC Application" tutorial on Codecourse's YouTube Channel.
I have moved the project to the root(htdocs)
and now when I load http://localhost all I see is the default apache folder index.
My files are organised as such:
htdocs
->app
->public
->.htaccess
.htacces contains:
Options -MultiViews
RewriteEngine On
RewriteBase /public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Edit: if I go to localhost/home (one of the controllers i setup) it loads the page correctly
The rewritable part http://localhost/ is empty, so it does not match (.+).
It would match (.*).

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