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

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]

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 URL Rewrite not working

I've never been good at .htaccess, I'm trying to copy and paste some code that worked on another one of my domains and modify it to work here. I will have several rewritten URLs, some static, some dynamic, but I can't even get the simplest of them to work. This one is testable here: http://lindseymotors.com/home
Clearly, index.php is available because if you access http://lindseymotors.com it works.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.* [NC]
RewriteRule ^home$ index.php
RewriteRule ^home/$ index.php
# When answering, if you could write a statement that would combine
# both of the statements above into one that would be appreciated.
As I said, these same conditions worked on another one my domains because I copied the code right over. I asked my server admin to double check everything on his end and it was fine. Any ideas?
Only thing I can think of is make sure the use of .htaccess is really on. The easiest way you can check since your server admin says it's fine is to put random text at the top of your .htaccess file. If your .htaccess file is being read and .htaccess files are enabled, it should throw a 500 internal server error. If not, then they don't have .htaccess files enabled and need to add AllowOverride All to the Apache config vhost.
Here is your rule combined into one as you noted. You really don't need the RewriteCond, but I will leave since you were using it previously.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.* [NC]
RewriteRule ^home/?$ index.php [L]

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.

htaccess - Script inside subdirectory

I have already one blog running in root folder and I want to test another one in a sub directory called "test".
How do I make all links point to example.com/test/login.php and not example.com/login.php ? I don't want to edit all links in my files, I want this one behave as root so later no need to change anything when I put it in production.
I assume it can be fixed with .htaccess crazy Rewrite Module but I haven't figured it out yet so please help save my time!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} /test/ [NC]
RewriteRule !^test/ /test%{REQUEST_URI} [L,NC,R]
Reference: Apache mod_rewrite Introduction

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