htaccess - Script inside subdirectory - apache

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

Related

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.

mod_rewrite on 1and1 shared hosting

I'm trying to do some URL rewriting, but my host (1and1) and my little experience with .htaccess files give me troubles.
Specifically, I am trying to setup the .htaccess file on /news directory, where I have a show.php file. My purpose is to convert www.example.com/news/blahblah to www.example.com/news/show.php?article=blahblah.
My .htaccess file so far is (EDIT: updated my .htaccess, still no solution):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/news/([a-z0-9_-]+)$ /news/show.php?article=$1 [L,NC]
</IfModule>
I have messaged my host for details on how to enable the module, and they replied that I should input these lines to enable the module.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
I tried with RewriteBase, and adding the rest lines to the file, but I got no results so far. I am new to .htaccess coding, so I don't know much about these commands, and tutorials don't seem to answer my questions, so any help understanding what I'm doing wrong is dearly appreciated.
EDIT: I updated the .htaccess I have on the folder. I also uploaded the support's file on the root folder, but no result. I still get 404 Not Found errors.
The lines that you've added aren't required. It seems maybe a couple of things may be the issue. Either mod_rewrite isn't loaded, thus the <IfModule> block is never executed. Or the htaccess file is being ignored.
You should be able to tell if you remove the <IfModule mod_rewrite.c> and </IfModule> lines, then try going to a page. If you get a 500 Server error, that means mod_rewrite isn't loaded and the server is puking because the rewrite statements aren't recognized. Otherwise, you can try adding some jibberish into the htaccess file and if you get no errors at all, then it would seem the htaccess file is being ignored.
Seeing as you're on shared hosting you'll need to contact 1and1 support. I have no idea how they run their shared hosting, but if the module isn't loaded, then they need to load the rewrite module, something like:
LoadModule rewrite_module modules/mod_rewrite.so
otherwise they need to make your document root overridable:
AllowOverride All
so that the htaccess file can rewrite the URI.
EDIT:
got to mention that I have no .htaccess on the root folder, just the /news folder.
This is what the problem is then. Your rule needs to be changed to:
RewriteBase /news/
RewriteRule ^([a-z0-9_-]+)$ show.php?article=$1 [L,NC]
Found the solution. I uploaded an .htaccess file at root folder containing only what support gave me, and also uploaded my own .htaccess file at the news folder, and it worked. The correct .htaccess of the news folder is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/
RewriteRule ^([a-z0-9_-]+)$ show.php?article=$1 [L,NC]
</IfModule>

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

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]

Mod Rewrite Hide Folder

I think this is a pretty simple question.
How do you an apache rewrite to hide a folder.
EX: www.website.com/pages/login.php to www.website.com/login.php
or www.website.com/pages/home.php to www.website.com/home.php
The folder needs to alway be hidden. thanks
I assume what you want is for the browser to request /home.php but the server to actually use the file located at /pages/home.php, right? If so, this should work:
Make sure the apache mod_rewrite module is installed. Then, use something like this in your apache config, virtual host config, or (less desirable) .htaccess file:
RewriteEngine On
RewriteRule ^/(.*)$ /pages/$1
The rules use regular expressions, so you may want to look at a reference on that topic if you're unsure. Read the manual for more info on other directives (RewriteCond can be very useful) or rule options.
I know the original post here was from a couple years ago, but it's been coming up first in the search engine, so maybe this will help others looking to hide a folder name in the URL.
Not exactly what original poster wanted, but along the same lines.
RewriteCond %{HTTP_HOST} ^mydomainname\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomainname\.com$
RewriteCond %{REQUEST_URI} !^/subfoldername/
RewriteRule (.*) /subfoldername/$1
The above example would redirect any request to mydomainname.com or www.mydomainname.com to the subfoldername directory in the root directory for the domain, and the subfolder name would not appear in the URL.
If your example actually reflects the files you need, then in your .htaccess file:
#Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/pages/(.+)\.php $1\.php [NC, L]
Also, if the directory has read permission, it cannot be, in reality "hidden". I assume you mean that it no longer appears in the url.