Jacob
Jacob

Reputation: 900

.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

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.

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.

Upvotes: 0

Views: 994

Answers (2)

Nawa Augustine
Nawa Augustine

Reputation: 39

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>

Upvotes: 1

Russell
Russell

Reputation: 69

Try this right after RewriteEngine on

RewriteRule ^(.*)/$ /$1 [L,R=301]

Upvotes: 1

Related Questions