Correct way to remove index.php in URLs - php

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]

Related

.htaccess redirect all urls to public/index.php without backslash on localhost

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>

.htaccess not working with php includes

I'm trying do use mod_rewrite at my .htaccess but isn't working.
my url is http://gestor.samfbas.com.br/index.php?p=something
it should be http://gestor.samfbas.com.br/something
The file is in a subdirectory (gestor) in my host, where all the files are.
<IfModule mod_rewrite.c>
Options +FollowSymLinks +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [L]
</IfModule>
Try this one (if index.php is in the root folder http://gestor.samfbas.com.br/index.php):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
This should work. Testet it here on my local maschine (No other server redirects or else, just a fresh xampp installation).
It redirect http://gestor.samfbas.com.br/something to http://gestor.samfbas.com.br/index.php?p=something without changing the url in the browser.
And additional to the question in the comment.
This URL part p= should not be known be outside users!
Better use a long var here like sadff34dngn4nil212ugn=, so nobody can call the index.php with parameters directly from outside. You can't prevent that 100% but the redirect parameter p= is only for internal use.
But its just my opinion on that.
Hopefully that helps a little.
Find the right way to rome ;)

htaccess to be used only in one folder

I've just started to use .htaccess and i'm having an issue here.
Inside my public_html folder i have these files.
index.php
profile.php
test.php
.htaccess
And when i go to profile.php file i have some parameters.
http://website.com/profile.php?id=1&name=Doe
For showing better SEO links i'm trying to make it appear like this
http://website.com/1/Doe
with this rewrite condition
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?url=$1 [QSA,L]
</IfModule>
depended on this answer (click here)
and then in my php file i get the id of the user so i can make the queries etc with this code
$path_components = explode('/', $_GET['url']);
$ctrl=$path_components[0];
But the thing is that if i do so every file in my folder is trying to make the Rewrite Rule but i want a specific file... the profile.php one.
EDiT 1:
So from the comment below i made this htaccess file, and regardless the mod_rewrite is enabled on my server, the link is not changing at all.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/profile.php$ [NC]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /profile.php?id=$1&name=$2 [L]
</IfModule>
And my php file now..
$path_components = explode('/', $_GET['id']);
$ctrl=$path_components[0];
But the link
http://website.com/profile.php?id=1&name=Doe
is not changing at all.
You need to redirect your old url to the new one, put this above your existing rule
RewriteEngine on
RewriteCond %{THE_REQUEST} /profile\.php\?id=([0-9]+)&name=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteRule ^([^/]+)/([^/]+)/?$ /profile.php?id=$1&name=$2 [L]
Hello the issue is simple: you should exclude existing files/folders from rewriting process.
I would recommend to have only one index.php page where you get all the request and process in one place. Anyway your working htaccess file should look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
# exclude real folders/files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^ index.php [L] - # redirect anything to index
# for your version
RewriteRule ^(.*)$ profile.php?url=$1 [QSA,L]
</IfModule>

Why do I need to add index.php in order to route my website correctly? [getkirby]

I uploaded the code to the server. It started the homepage correctly . But when I press any link , I get 404 not found error. I discovered that I need to add index.php to my url for it to work.
so it will be like that:
mydomain.somee.com/myWebsite/index.php/anotherPage
When I was working locally using Xamp as a server, I didn't get any of those problems.
I got those problems after I uploaded the website to some.com which apparently doesn't use .htaccess file (editing or removing has no effect).
How to add this index.php automatically and hide it from the user?
I didn't change any of the system files or the htaccess
please tell me if you need anymore files or description.
You need to redirect all your all pages through index.php file but remove it from URL.
Write below rules in your root .htaccess file:-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
OR
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
To understand, How htaccess rules are working, This link will help you :)
Hope it will help you :)

.htaccess Root Rewrite Without Folders

I've spent countless hours searching for similar issues and found the solutions to not work for me. I have an index.php with an 'id' GET variable to load pages dynamically as mydomain.com/[idVariableHere].
My .htaccess code works:
RewriteEngine On
RewriteBase /
RewriteRule ^(js|css|images)($|/) - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [L,QSA]
However, it's not preventing the /js/, /css/, and /images/ folders from being displayed. It's interfering with the last RewriteRule and I have no idea how to get around this.
My goal is to keep the mydomain.com/[idVariableHere] look with restriction on these important folders. Please help.
Have it this way:
ErrorDocument 404 default
ErrorDocument 403 default
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?id=$1 [L,QSA]
From what I understand, you want an .htaccess to prevent access to /js/, /css/, and /images/ folders. Assuming these are found in the root. Just create an .htaccess file in each folder with the following code:
deny from all
This was taken from:
deny direct access to a folder and file by htaccess

Categories