Reputation: 900
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
No matter how many configurations I try, I can't seem to avoid some sort of weird glitch with the URL.
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.
RewriteEngine On
RewriteBase /time-tracker/
RewriteRule ^public\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /public [L]
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.
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
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]
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.
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
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
Reputation: 69
Try this right after RewriteEngine on
RewriteRule ^(.*)/$ /$1 [L,R=301]
Upvotes: 1