.htaccess simple mod_rewrite sends 404 error - apache

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]

Related

Setting up .htaccess with apache and laravel

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.

Changes to RewriteRule in .htaccess not taking effect

I had this rewrite rule set up in .htaccess and it was all working fine...
Options +FollowSymLinks +ExecCGI
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/mypage(.*)$ [NC]
RewriteRule ^(.*) http://example.com/PHProxy/poxy-0.5b2/index.php?url=http://example.org/mypage [L,R=302,NC]
However, when I change the url in the RewriteRule to
http://example.com/PHProxy/poxy-0.5b2/index.php?url=http://example.org/mypage it still redirects to the old URL.
After some research, I added a syntax error into the .htaccess file to check the .htaccess file was being used (and indeed it was - as it resulted in an Internal Server Error when you tried to load a page from that directory).
There seems to be some caching somewhere, but I'm not sure. Any ideas why my change is not being picked up / how to troubleshoot and resolve?
Problem solved. Just noticed that there is a mypage subdirectory which still contained the old rewrite rule, so that was the one being executed.

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

.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)!)

Apache Mod Rewrite -- fake a folder when infact there is one

I have, let's say, www.website.org/folder/ which inside has the following .htaccess
RewriteEngine On
RewriteRule ^folder/[0-9]+ http://www.website.org/folder/index.php?n=$1 [NC]
inside folder I have many folders like 1234, 4567, etc. The behavior I'm looking for is a rewriting from www.website.org/folder/1234 to www.website.org/folder/index.php?n=1234. However, for some reason the rewriting doesn't occur and I get a Forbidden error (given that you can't access the directory itself).
How can I solve this?
Thank you very much
-- Note: I had to put away Options +FollowSymlinks because I was getting a Option FollowSymLinks not allowed here error from the provider's webserver.
-- Edit 1
Following Jason's post I modified the .htaccess as follows (I still kept it in folder):
RewriteEngine On
RewriteBase /folder/
RewriteRule ^folder/([0-9]+)/?$ /folder/index.php?n=$1 [NC,L]
But it still brings me to the folder, why is this? Thanks!
The way your rule is written, .htaccess should be in your webroot not the folder directory.
Alternatively, you could modify your RewriteBase. However, I'd do the above and use:
RewriteEngine On
RewriteBase /
RewriteRule ^folder/([0-9]+)/?$ /folder/index.php?n=$1 [NC,L]