Setting up .htaccess with apache and laravel - apache

This seems to have been asked hundreds of times, and I read it and tried different things. Nothing seems to work, so after reading about 5 google pages about this I gave up and I want to see if anyone can give me an answer.
I want to remove the /public from my URL
I'm using an apache 2.4 server on windows. I just installed laravel.
I made sure the rewrite module is on in apache (php's get_apache_modules() shows it) and now I'm trying to write the .htaccess - and I can't understand how it works.
I don't want to change anything in my apache configurations (I'm using this machine to develop multiple apps)
my current .htaccess files are :
in the app route directory
Options -MultiViews
RewriteEngine On
RewriteRule ^ public/index.php [L]
and in the public folder
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /texteditor/public/index.php [L]
Right now localhost/texteditor/ - and anything after it that isn't public/ gives me 404
Also can anyone recommend an easy .htaccess tutorial for dummies?

I have done this thing by doing following and working fine.
Please do this if it is helpful to you.
Move public/index.php to www/texteditor/index.php.
also Move public/.htaccess to www/texteditor/.htaccess.
then you can do it.
Please replace your app root directory's .htaccess with public/.htaccess.

Related

.htaccess simple mod_rewrite sends 404 error

I've created an .htaccess file in a shared server, inside a folder called API that is a child of the root folder.
I've placed the following simple code inside the file
Options +FollowSymLinks
RewriteEngine On
RewriteBase /API/ # also tested with / alone (edit) also tested removing-it
RewriteRule ^/API/(\d+)*$ /API/index.php?i=$1
My intentions is to turn /API/{var} into /API/index.php?i={var}, but the trick ain't working at all. What can be causing the issue, since every query var I sends me into 404.
What can be causing the problem? Where should I start debugging?
Edit:
After several failed attempts I'm gonna try using the FastRoute php library as an alternative, since this .htaccess issue seems unresolvable.
Thanks FĂ©lix for all the help in the chat.
The problem is probably the leading slash / and API in your rewrite pattern, Remove them from there
Try :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /API/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)*$ /API/index.php?i=$1 [NC,L]

htaccess doesn't load new js/css from another vhost

Ok, so here it is!
I have two virtual hosts, named main.aaa.com and static.aaa.com.
I have a htaccess rule in main.aaa.com that for each path that contains js|css|img|ico will load them from static.aaa.com.
This is the htaccess for main.aaa.com:
Options +Indexes
Options -MultiViews
Options +FollowSymLinks
# Turn on the RewriteEngine
RewriteBase /
RewriteEngine On
#
# Rules
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(css|js|ico|img)$ index.html [PT,L]
RewriteRule ^(.+\.(css|js|img|ico))$ http://static.aaa.com/$1 [L]
What's going on is that everything works perfectly, absolutely perfect, but...
if I create a new file in static/js folder and try to load it in main.aaa.com index file, it just gives a 200 OK, file not changed, and by looking into it it seems that htaccess doesn't even bother to look for the file in static vhost.
I have changed file permissions to 755, all of the files have the same permissions and the same owner, only that the new ones that I create are not loaded.
Any help would be much appreciated! Thank you!
[EDIT]: I tried also to rename a file that is now correctly loaded, because I thought I am missing something about file permissions. Stopped working. Renamed it back to previous name and it loads. So, I am thinking, is there a history of files or something that I can clear? I am at the point to break something, anything!
Using ubuntu 14.04 LTS and apache 2.4 if that helps.
I have found a solution to my problem, I don't really understand why it works, I just hope it does not introduce any other problems when I push my files to my hosting.
So here it is, if anybody is as stupid as me :), I have changed and moved this line:
RewriteRule ^(.+\.(css|js|img|ico))$ http://static.aaa.com/$1 [L]
above this line
RewriteRule !\.(css|js|ico|img)$ index.html [PT,L]
the final result being this one:
RewriteRule ^(.+\.(css|js|img|ico))$ http://static.aaa.com/$1 [R,L]
RewriteRule !\.(css|js|ico|img)$ index.html [PT,L]
Good luck!
EDIT: maybe the answer is that redirects, with R flag, must be written first. Maybe.

My htaccess RewriteRule works on localhost/mysite but not at mysite.com on a 1&1 shared host

Solved:
unfortunately, the solution is not a satisfying one. This morning, when trying #Wige's Suggestion, I found, to my suprise, that the Expected values WERE infact sent to the page as a GET query. Apparently, 1&1 (who I know have been making changes to their environment this last couple weeks), did something behind the scenes which magically fixed my problem, and now all of my previously unworking code is working as originally expected.
New info: The Apache version of the production server is 1.3.34 vs 2.2.21 on my localhost.
I'm having trouble figuring out why my RewriteRule is not working properly in production.
RewriteRule ^page/pretty-url/(.*)$ page.php?query=$1 [L]
In my local testing environment (localhost/mysite/page/pretty-url/{...}) it works fine, but on mysite.com/page/pretty-url/{...} it doesn't work properly. It loads page.php as expected but apparently the ?query=$1 piece is ignored ($_GET is empty)
I imagine that the problem is somehow related to the server configuration. I'm on a 1&1 shared hosting account with no httpd.conf access.
What that RewriteRule does (or should do):
I want urls like
*example.com/page/pretty-url/{{info_for_dynamic_content}}
to be rewritten to
*/page.php?query={{info_for_dynamic_content}}
So I can access info_for_dynamic_content
within php as $_GET['query']
The full .htaccess file for reference:
AddHandler x-mapp-php6 .php
DirectoryIndex index.php
ErrorDocument 404 /index.php
Options +FollowSymLinks
# per #Jacques Chester's suggestion
Options -MultiViews
RewriteEngine on
RewriteBase /
# the rule in question
RewriteRule ^page/pretty-url/(.*)$ page.php?query=$1 [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule (.*) /index.php [L]
Most likely, your host is storing the variables somewhere else. I would add a call to phpinfo(); into your script and go through the environment variables there to see if you can find the values that should have been in get.
Seems to be connected to 1&1's hosting environment.
See this question, in particular, this answer.
Basically it appears that 1&1 enable "MultiViews". By adding
Options -MultiViews
You disable that setting for your website and according to various reports, this resolves the issue.
I struggled with RewriteRule issues on a 1&1 / 1and1 / IONOS shared server for WEEKS and eventually I found the perfect setup for a 1 and 1 shared server, start your .htaccess file like this
Options -MultiViews
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
I hope this helps someone as 1&1 are useless when it comes to htaccess support

Invisibly rewriting root to subdirectory using mod_rewrite (ajaxplorer)

I've been looking on various sites reading mod_rewrite examples for a few hours, and have used it many times before successfully... but I'm trying something new with it now and can't for the life of me get it working!
I'm using a PHP file manager tool called AjaXplorer, I have it in a subdirectory on the root of my server /ajaxplorer-core-4.0.4. When I go to the root of my site http://domain.com/ I want it to invisibly redirect to the /ajaxplorer-core-4.0.4 folder (but still show the root domain in the address bar).
I still want to be able to access the other files/directories on the root as normal just typing in the path.
I assume this is possible? Seems relatively simple but I just can't get it working.
AjaXplorer seems to load js files and images etc from /ajaxplorer-core-4.0.4/plugins, I have a feeling that's where it's tripping me up.
Any pointers would be massively appreciated! Thanks
Found it on another answer luckily :)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteRule ^(/)?$ subdir/index.php [L]
Here's the wiki of serverfault.com
The howto's htaccess official guide
The official mod_rewrite guide
And if that's not enough:
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

.htaccess mod_rewrite subdirectory to URL parameter

I hope this was not asked over and over again before, but I didn't get further to an answer using google, w3schools and so on. So here is my question:
I'm writing a script that creates kind of an index of projects that I have on my homepage and makes a nice list with images and teaser text based on an info file. I mostly have my projects on github and the readme is in markdown so I thought I could dynamically generate the HTML from the markdown of the latest blob from github on demand using PHP so it gets updated automatically.
My directory structure looks like this:
projects
project1
.remoteindex
.info
project2
.remoteindex
.info
index.php
.htaccess
So when just domain.tld/projects/ is requested I get the info from .info and make a nice index of all projects. But if domain.tld/projects/project1/ is request it, I want to internally redirect it to domain.tld/projects/?dir=project1 to do my markdown parsing and so on. But domain.tld/projects/project1/image.png should not be redirected.
This is what I tried:
.htaccess
RewriteEngine on
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]
I made it redirect instead of rewrite so that I can see what the error is because I just got an 404. The URL that I get redirected to is domain.tld/home/www/web310/html/projects/index.php?dir=project1 so obviously there is something going wrong with the internal structure of the web server an paths an whatever.
I hope you can understand my problem an I would be very pleased if someone could help me, because I'm totally lost on .htaccess anyway.
Edit:
See my answer below for the used .htaccess.
The strange thing is that if I have an index.html in on of the subdirectories, my local web server (Apache with XAMPP for Mac OS X 1.7.3) does not rewrite and the index.html gets displayed, without one it works correctly.But on my real web server that serves my homepage it rewrites both with and without index.html (which is what I want). Any hints on that?
Thanks for all the help so far! You guys are just awesome!
I figured out that a symbiosis of both of your solutions works well for me:
RewriteEngine on
RewriteBase /projects
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [QSA,L]
Of course only without [R], this was my fault. (See my question edit for another question please).
You need to add a RewriteBase /projects to the htaccess.
That way the redirect will work properly.
Edit:
RewriteEngine on
RewriteRule ^index.php - [L]
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]
Following what you ask, this is important not to do a redirect, but let the rewriterule modify internally the URL i.e. the RewriteRule should not end with [R,L] but rather [L] and maybe the "query string append" directive to keep what's after the ? so this should probably be [QSA,L].
Now here's how I'd do to avoid rewriting static files: if it's not a file then (and only then) test it:
# if it's not a file...
RewriteCond %{SCRIPT_FILENAME} !-f
# ... and it's a dir
RewriteCond %{SCRIPT_FILENAME} -d
# ... then rewrite it internally and stop further processing:
RewriteRule projects/([^/]+)(/?)$ index.php?dir=$1 [QSA,L]
And now two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)