mod-rewrite RewriteBase not working correctly from root - php

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 (.*).

Related

Laravel 8.4 – Remove Public from URL

I am working on creating a website that converts text into speech. I am using the script from codecanyon which is Developed with PHP 7.4.x and Laravel 8.4.x . The issue I am facing is that I don't want to show /public URL as a permalink to access the website. Now, if I type mywebsite.com/pubic then the website is accessible otherwise if I write mywebsite.com then it only shows the files. I followed some of the previous queries posted on StackOverflow but got no results.
The topic I followed is Laravel 5 – Remove Public from URL
I tried even by renaming server.php , copying .htaccess and rewriting it. But those all resulted in a screen showing up errors or even blank.
My .htaccess file is;
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
To remove the /public from the URL you are not going to use the .htaccess file.
What you are going to do instead is set the /public folder as the serving directory for your Server.
If you use Apache, just go to your /etc/apache2/sites-available/000-default.conf file and change the DocumentRoot from {your base folder}/ (usually /var/www) to {your base folder}/public
The content of public folder should go to the public folder of your server (fe it's public_html on my server), and everything else should be stored in a different folder (fe laravel_mywebsite). Then you should go to public_html/index.php and change the folder paths based on where you put your Laravel app.
If you want to only use .htacces, you just need to rewrite all requests from /public to /
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,R=301]

htaccess redirecting to files available in the public directory

[ Background ]
This is the structure to my application:
app/
controllers/
...
modules/
...
views/
...
public/
css/
main.css
images/
...
js/
...
index.php
.htaccess
For various reasons (security, organization, etc.) the public application (website) is loaded and displayed by public/index.php At the "root" of my server I have the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /BU/cs-602/developer-story/
RewriteRule ^(\/||index\.*)$ public/index.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RedirectMatch ^(/BU/cs-602/developer-story.)([\w\d\s\/\-\:\_%\ ]*\.css)$ $1public/$2 [L]
RewriteRule ^([\w\d\-\_\/\ ]*)$ public/index.php?p=$1 [L,QSA]
</IfModule>
These rules should rewrite/ redirect all requests from the server "root" to public/index.php Since I am developing locally on my development server this project is not actually at the root of my server but is located at /BU/cs-602/developer-story/ This is why I added the RewriteBase rule.
If I remove the RedirectMatch rule everything from an MVC standpoint works:
URLs like http://localhost/BU/cs-602/developer-story or http://localhost/BU/cs-602/developer-story/ become http://localhost/BU/cs-602/developer-story/public/index.php AND URLs like http://localhost/BU/cs-602/developer-story/user/id become http://localhost/BU/cs-602/developer-story/public/index.php?p=user/id.
Here is a live htaccess test.
[ Question ]
How can I fix the .htaccess file or the RedirectMatch rule to redirect request to resources like CSS or JS files? The current rule works in the tester but not on my server unless I remove the RedirectMatch rule.
I would like a URL like http://localhost/BU/cs-602/developer-story/css/main.css to become http://localhost/BU/cs-602/developer-story/public/css/main.css that way in my HTML I do something like:
<img src="images/logo.png">
The server actually loads:
<img src="public/images/logo.png">
This way I do not have to prefix all my resources with public/.
[ Disclosure ]
This question is for help with a final project at my University, but this type question is allowed and not violating academic policies. Making a project with htaccess and MVC is not a requirement, just something I'm doing to challenge myself.
UPDATE:
Here is my original RedirectMacth rule. Anything that is not a PHP or HTML file is redirected:
RedirectMatch ([\w\d\s\/\-\:\_%\ ]*\.(?!php|htm|html)) $1 [L]
I found the solution after reviewing the error logs on my server. My various attempts were causing redirect loops. Here is my working .htaccess file in case it helps anyone trying to do something similar.
The key is to short circuit the .htaccess file when necessary so it doesn't keep rewriting your URLs infinitely. I added inline comments to explain the process:
# Redirect all requests to the public directory.
<IfModule mod_rewrite.c>
RewriteEngine On
# Only needing for local develoment in my case.
RewriteBase /BU/cs-602/developer-story/
# If nothing or an index page is requested go to `public/index.php` and stop processing.
RewriteRule ^(\/||.*index\..*)$ public/index.php [L,QSA]
# If the URL already contains the `public/` directory go to URL and stop processing.
RewriteRule ^(.*public\/.*)$ $1 [L,QSA]
# If it appears this URL is for a resource (JS, CSS, etc.) prefix with `public/`.
RewriteCond %{REQUEST_URI} ^([\w\d\s\-\_\/\%]*)\.(?!php|htm|html).*$
# Go to the modified URL if the last rule triggered and stop processing.
RewriteRule ^(.*)$ public/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# Rewrite all remaing URLs to our apps MVC structure.
RewriteRule ^([\w\d\s\-\_\/\%]*)$ public/index.php?p=$1 [L,QSA]
</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.

Unable to open a controller(PHP web page) in 000webhost.com. "error404.000webhost.com/"

I have just uploaded all file of my test website to 000webhost.com domain.
My site is farazphptest.comxa.com
I was working on Codeigniter before uploading the site. All of my controllers are working fine on my localhost, but after uploading the site on server when i try to access a controller say farazphptest.comxa.com/welcome where "Welcome" is my webpage/controller, it does not work and opens "error404.000webhost.com/?" All of my controller including "welcome" are working fine on localhost, but after uploading website on domain only the home page loads perfectly.
This is the 1st time i have put a site on web, do tell me if i am missing out some tricks. I have just simply copied my file on web as stated. Thanks
Here is my .htaccess file used in both for localhost and on server.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /faraztest
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /faraztest/index.php
</IfModule>
An alternative solution is to create sub-domain on 000webhost.com with project name , and call the project from sub domain url Directly, it will be like this :
From Cpanel got to Subdomains
Create new subdomain with project folder name. ex : test.farazphptest.comxa.com
Overwrite the created subdomain folder with your project folder. ex : test
Edit your .htaccess in your main uploaded project folder to be like this :
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
now you can call your project directly from the new subdomain url.
ex : http://test.farazphptest.comxa.com
Following solved my problem
My .htccess file is
# index file can be index.php, home.php, default.php etc.
DirectoryIndex index.php
# Rewrite engine
RewriteEngine On
# condition with escaping special chars
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
and it worked like charm!

Redirect all requests to a directory and it's subdirectories to a file inside the directory, using Apache2 server

The Document folders are structured like this
/var/wwww/ (root)
java/
...
php/
projectone/
controller/
view/
index.php
...
projecttwo/
...
I want all requests to /php/projectone or it's subfolders to redirect to /php/projectone/view/index.php, while requests to /php/projecttwo and other folders should not be redirected.
I believe using mod_rewrite is the way, however, I still haven't achieved what I want.
Place an .htaccess inside project one folder with the following rule:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /php/projectone/
RewriteRule ^(view/)?index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . view/index.php [L]
And you can repeat that for any other project folders you have. by changing the RewriteBase.

Categories