For a customer of mine I'm building a website. For this I use the Laravel framework.
Now I know the best practice is to set the public directory as DocumentRoot, but the problem is, I can't.
So I've done some research, and I saw that you could use .htaccess to handle this little problem.
The snippet that's on the laravel forums is the following:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
But when I try this, I get a 500 Internal Server error.
I found that the problem was the (.*) part of the fourth line... But I just can't see what's wrong with it.
So does anybody know what's wrong with this snippet, why am I getting a 500 error...
You're almost there. REQUEST_URI includes the leading slash /. So you must say
RewriteCond %{REQUEST_URI} !^/public
Don't change the public folder path, just copy the usual laravel tree and use the following in
/public/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
This worked for me on L3.
Related
Background
I'm creating a time tracking app with PHP on my localhost (MAMP). The app structure is as follows
htdocs/time-tracker/public/index.php
Issue
No matter how many configurations I try, I can't seem to avoid some sort of weird glitch with the URL.
What I need
I want the following result. When I visit the url 127.0.0.1:8888/time-tracker on my local machine, I trigger the php app, routing all requests through the htdocs/time-tracker/public/index.php. Preferably without a trailing slash, but priority is just to get the app to work.
My current .htaccess file
RewriteEngine On
RewriteBase /time-tracker/
RewriteRule ^public\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /public [L]
Updates
1. $_GET parameters change outcome
For some reason http://127.0.0.1:8888/time-tracker?debug=true and http://127.0.0.1:8888/time-tracker get me different results.
http://127.0.0.1:8888/time-tracker?debug=true results in a redirect to http://127.0.0.1:8888/public
http://127.0.0.1:8888/time-tracker results in a redirect to http://127.0.0.1:8888/Users/etc/etc/htdocs/time-tracker/public
Neither of these results are what I want.
2. Partially working
This .htaccess file has gotten my redirects to work whenever I put something in the $_GET
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php [L]
For example, 127.0.0.1:8888/time-tracker/?test=test works while 127.0.0.1:8888/time-tracker/ still redirects to http://127.0.0.1:8888/Users/etc/etc/htdocs/time-tracker/public
3. Not redirecting properly on root
The redirects works on all paths except for the root path. For example, 127.0.0.1:8888/time-tracker/test and 127.0.0.1:8888/time-tracker/?test=test both work, just not 127.0.0.1:8888/time-tracker/
I don't know why my regex won't pick this up. Here is my code
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* public/index.php [L]
4. Seeing an empty path name
I've tracked it down to one last issue: empty paths don't register with the redirect.
# Settings
Options +FollowSymLinks
DirectorySlash Off
RewriteEngine On
# Rules
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*|x) index.php?/var=$1 [L]
For some reason, it just can't catch the redirect if the path is empty.
5. Close enough solution
This is the best I got. This is actually working, but I couldn't fix the trailing slash issue.
# Settings
DirectorySlash On
RewriteEngine On
# Rewrite
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?/var=$1 [L]
Hopefully somebody can come solve the trailing slash directory root issue or at least confirm that it is impossible. At this point, the correct answer goes to anyone who can explain my mistakes and make this into a helpful post.
Try this right after RewriteEngine on
RewriteRule ^(.*)/$ /$1 [L,R=301]
Try this. Put your .htaccess file in the time-tracker folder
RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I'm aware of a lot of solutions around there, and tried everything. The problem is, when I put a .htaccess in the codeigniter subfolder, it's not read. I thinks it's because of the AllowOverride directive, but I can't change that.
I tried to skip the main subfolder that is not part of wordpress, one I called "sandbox", in the root htaccess (that's part of wordpress), so it doesn't "inherit" any problem from that rewrite (for example, to not have 404 managed by wordpress in the codeigniter project).
The problem is, as I said, that the .htaccess in the /sandbox/ciproject/ folder is not read. So, I tried this in the main .htaccess, and it worked... for a little time.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?sandbox/
RewriteRule . /index.php [L]
RewriteBase /sandbox/ciproject/
RewriteCond $1 ^(application|system|private|logs)
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|img|fonts)
RewriteRule ^(.*)$ - [PT,L]
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
That allowed me to make it work for a while... but it's not working anymore. Something more I could try? I already changed the config of the codeigniter project so it doesn't use index.php, but nothing. I only get a server error 500 now, and if I delete the second half of the htaccess, I only get 404 as a response for getting rid of the index.php
I don't know if I'm losing some data, but just ask and I'll edit this, so you're not blind at this one.
Based on my understanding, your code-ignitor part doesn't work because, you have two clashing rewrite rules in your htaccess.
RewriteRule ^(.*)$ - [PT,L]
RewriteRule ^(.*)$ index.php/$1 [PT,L]
These are a clashing rule set. Remove one of the lines and then try. It should work.
Use only this code in htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sandbox/ciproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
I'm currently learning fuel php and I've searched a lot in google all day long and still can't find a solution on how to remove index.php in URLs. That is, instead of
localhost/project/public/index.php/hello
I only need to write:
localhost/project/public/hello
I'm currently doing the tutorial here and when I clone their repository I'm getting a 404 error not found due to there URL having no index.php. How can I do that? I'm using WAMP server.
Create an .htaccess in the top level directory and add:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
</IfModule>
Try to add this .htaccess file to your web directory:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php [NC,L]
I'll preface this by pointing out that I'm a massive noob with .htaccess stuff and server related stuff in general.
So I've built a website locally with wamp and I used a .htaccess file that I found from googling around.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my_project_folder_name/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I obviously replaced "my_project_folder_name" with the real folder name. This works perfectly on my local server. I decided to upload to site to my Nearly Free Speech server and I have been having some trouble with the mod_rewrite. I have no idea what I'm supposed to replace "my_project_folder_name" with, and I can't get it to work. I tried leaving it blank, and that worked, except for some reason this caused a bug that prevented POST data from being received by my scripts. Well, I'm assuming that's what caused the bug.
Could someone please point me in the right direction?
For help purpose just check.
Check apache setting is rewrite_module setting is correct ?
if your folder name consist of two seperate words such as " XYZ ABC" then try " XYZ_ABC" and do the same with you .htaccess file.
Looking at the file it belongs to codeigniter (PHP FRAMEWORK). So if you are not using it then try to use..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /my_project_folder_name/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Hope your problem will get solved.. :)
I am quite new to PHP and just getting started with mod_rewrite. I know the basic lingo but come stuck when I want to simply refer to the route directory
i.e. this is not probs
RewriteRule ^settings/$ settings.php
[QSA,L]
But how to for example make:
RewriteRule ^page/(.*)$
index.php?Page=$1 [QSA,L]
which generates /page/[page-name]
Just become
/[page-name]
?
Maybe I didn't understand you but it seems that you need such .htaccess file to solve your problem.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Ignore valid files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Page=$1 [QSA,L]
</IfModule>
This should do it:
RewriteRule ^(.*/)$ index.php?Page=$1 [QSA,L]
However, you should place that rewrite rule after all the other specific rewrite rules you have, otherwise all requests will be redirected to index.php?Page=....