htaccess rewriterule to send a file to another file in the same directory - apache

I have a directory that contains an index.php and an index.html file, both being published from a CMS. There's a specific IP address that will attempt to access index.html, but should instead be shown index.php in the same directory. All other traffic should act as normal.
I've been working with some variations of this code:
RewriteEngine On
RewriteCond %{REMOTE_HOST} ^123\.456\.789\.10
RewriteRule ^index\.(htm|html?)$ index.php [NC,R=301,L]
This does do the redirect, but of course it goes to the root of the site rather than staying in the same directory. It's somewhat unclear what the directory path will be in all cases, so I'd like to tell Apache to stay in the same directory it's in.
Is this possible?
Thanks,
Jonathan

If you want to have this rule for IP: 123.456.789.10 then you shouldn't have this IP with
negation like: RewriteCond %{REMOTE_HOST} !
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subdir/
RewriteCond %{REMOTE_HOST} ^123\.456\.789\.10$
RewriteRule ^index\.html?$ index.php [NC,R=302,L]

Related

How do I write a mod_rewrite so that it doesn't affect a subdomain that is in a subfolder

I have a site where the frontend is on the main domain and the backend is on a subdomain whose document root is a subfolder of the main domain's document root.
I have added this:
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
to the .htaccess file for the main domain because it was giving a 404 error for refreshes and direct access. The problem is that adding that config results in a 500 error on my backend. How can I solve this?
You shouldn't necessarily need to do anything with regards to the subdomain, depending on the type of requests you are making (which you've not stated).
However, you can disable mod_rewrite for the subdomain by creating an additional .htaccess file in the root of the subdomain (ie. in the subfolder off the main domain's document root) and place the following:
# /subfolder/.htaccess (subdomain)
RewriteEngine Off
.htaccess files are inherited along the filesystem path, so the .htaccess file in the root of the main domain will certainly be processed when accessing the subdomain, except that the conditions (RewriteCond directives) should already exclude any requests for actual files/directories.
So it turns out I only needed to add one line to exclude the affected subdomain.
RewriteCond %{HTTP_HOST} !example\. [NC]
That did the trick.

.htaccess rule for redirecting to parent

I am trying to redirect one of my urls to the parent folder using .htaccess file. I have tried the following rule
RewriteRule ^test/(.+)$ /test/ [L,R=301]
found from htaccess wildcard redirect to parent folder but it is not working (logs show too many redirects).
I also tried the other rules below but none of them worked
RewriteBase /
RewriteCond %{REQUEST_URI} !=/test/
RewriteRule ^(.*) /test/ [END,NC]
or
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^test/(.+)$ /test/ [L,R=301]
The OS is ubuntu server. Any help or pointers is appreciated. Please let me know if I can furnish any other details to debug. Thanks
Following should work considering the parent directory is test
RewriteEngine On
RewriteRule ^(test/).* /$1 [R=301,L]
Add this to disable MultiViews:
Options -MultiViews
The Apache docs on mod_negotiation, describes what the Multiviews Option does, when enabled:
If the
server receives a request for /some/dir/foo and /some/dir/foo does not
exist, then the server reads the directory looking for all files named
foo.*, and effectively fakes up a type map which names all those
files, assigning them the same media types and content-encodings it
would have if the client had asked for one of them by name. It then
chooses the best match to the client's requirements, and returns that
document.
Use:
Options -MultiViews
RewriteEngine on
RewriteRule ^test/(.+)$ /test/ [NC,L,R=301]
In your specific folder
RewriteEngine On
RewriteRule ^rootFName(.*) /rFile.php [QSA,R=301,L]

.htaccess redirect sub directory access attempts back to root

I wish to protect folders such as /css and /js from listing directory contents to snoopers. Perhaps I could do this via PHP, but I need to use .htaccess.
Desired:
Anyone browsing to http://example.com/css should not see a listing of files, but is immediately redirected back to http://example.com.
I have tried several variations on the following, but cannot seem to get it right:
.htaccess inside each private folder:
RewriteEngine On
Redirect 301 /. http://example.com
or, in the webroot:
RewriteEngine On
RewriteBase /
RewriteRule /(.*)$ /$1 [L,NC,R]
Note: I already have an .htaccess file in the webroot, which any additional mod_rewrite commands would need to accomodate:
RewriteEngine On
RewriteBase /
RewriteRule ^dev/(.*)$ /$1 [L,NC,R]
RewriteRule ^/(.*)$ /dev/$1 [L,NC]
You can create /css/.htacces with this line:
ErrorDocument 403 http://example.com/
Options -Indexes
Do same in /js/.htaccess file.

www I added to HTTP_HOST via htaccess file disappears when redirecting to second htaccess file

I am struggling with an htaccess file. Actually, it is two files.
But first things first, so my questions are:
1) Why does my .htaccess(1) file add the www at the beginning of the HTTP_HOST and the slash at the end of folder REQUEST_URI IF AND ONLY IF the .htaccess(2) file is not there (deleted or renamed)?
2) What is wrong with the RewriteRule and conditions that I wrote in .htaccess(2) to redirect the REQUEST_URI to /publicfolder/REQUEST_URI? Conditions doesn't seem to work and when I surf to domain.com/nonpublicfolder it goes to domain.com/domainfolder/publicfolder/nonpublicfolder.
My website is structured as follows:
/
.htaccess(1)
domainfolder/
.htaccess(2)
publicfolder/
genericfolder/
index.extention
file.extention
nonpublicfolder/
So I have one htaccess file in the root folder ( .htaccess(1) ) where I:
add 'www' at the beginning of the HTTP_HOST;
add '/' at the end of REQUEST_URI if it does not end with a file extension;
redirect domain.com/anyfolder/anyfile.extention to domain.com/domainfolder/anyfolder/anyfile.extention;
like so:
<IfModule mod_rewrite.c>
# System symbolic links are allowed.
Options +FollowSymlinks
# Runtime rewriting engine enabled.
RewriteEngine On
# HTTP_HOST starts with 'www'.
RewriteCond %{HTTP_HOST} ^(?!www\.) [NC]
RewriteRule ^.*$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NC]
# Folder requests end with '/'.
RewriteCond %{REQUEST_URI} ![^/]+\.[a-zA-Z0-9]+$ [NC]
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,NC]
# Files and folders are in the 'domainfolder' folder.
RewriteCond %{REQUEST_URI} !^/?domainfolder/ [NC]
RewriteRule ^.*$ /domainfolder%{REQUEST_URI} [R=301,NC]
</IfModule>
And then I have my .htaccess(2) file - in the domainfolder folder - where I redirect files and folders requests to the publicfolder folder IF AND ONLY IF they are not pointing to the notpublicfolder folder or to the Google Site Verification file:
<IfModule mod_rewrite.c>
# Runtime rewriting engine enabled.
RewriteEngine On
# Public files and folders are in the 'publicfolder' folder.
RewriteCond %{REQUEST_URI} !^/?domainfolder/publicfolder/ [NC]
RewriteCond %{REQUEST_URI} !^/?domainfolder/nonpublicfolder/ [NC]
RewriteCond %{REQUEST_URI} !^/?domainfolder/googlexxx.html$ [NC]
RewriteRule ^/?(.+)$ /publicfolder/$1 [R=301,NC,L]
</IfModule>
Thank you very much for your time and patience.
(1) mod_rewrite does multiple passes and on each by default it will only open the first .htaccess file it finds walking up the folder hierarchy from the requested file. read my Tips for debugging .htaccess rewrite rules for more discussion of this. Yes you can use a Options setting to change this behaviour but this has a performance hit and I would suggest that you avoid doing so.
(2) When using hierachical .htaccess files, mod_rewrite has to associate URI path to the current directory and can get this wrong. The RewriteBase directive tells mod_rewrite what the true association is, so use this.
Rule order is important. If you don't have a local Apache instance where you have root privilege and can enable rewrite logging, you need to build up your access file(s) incrementally rule-by-rule, testing at each step because you only get a work/doesn't work return. Again my tips explains how to do this.

.htaccess, Laravel & Ikonboard

I have to admit, creating mod-rewrite rules still confuses me! So, I'm after some help please...
I've taken on a site built in Laravel, but now need to add an existing forum into the domain. The forum is Ikonboard, which on the live site lives in the cgi_bin folder. When I copy this to the new site, I can't get access because the htaccess is rewriting ALL URL's to the public folder (where Laravel want's it).
So, how can I make any requests to the cgi_bin folder work as well as keeping the rewrite to public for Laravel?
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Rewrite to 'public' folder
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]
</IfModule>
Thanks
Simple:
RewriteCond {%REQUEST_URI} !^/cgi_bin
Add this above your other REQUEST_URI rule. If cgi_bin is in the URI, then it will stop rewriting.
Edit - Based on your response with regards to the directory structure, it seems to be wrong. Your www directory is your public directory. So, your structure should look like this:
/www/ (your public folder)
bundles/ (etc...)
cgi_bin/
.htaccess
index.php
/laravel/
(etc...)
Change your directory structure to look like that, and make sure that your .htaccess file has the following in it:
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you call a CGI document, it should pass through because %{REQUEST_FILENAME} !-f is set above. All assets should pass through as well. You do not need to specify whether or not the cgi_bin folder is being requested. If it does not work, then add the line as I had originally in this answer (except, you would put the rule just under RewriteEngine On.