Mod_Rewrite rewriting directory I dont want it to - apache

So I am using Kohana which is useful if you know it, but not needed to assist me.
I have the following mod_rewrite rules:
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/store/.*$
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php [L]
So I am trying to rewrite all requests for files and directories that do not exist to index.php.
However, I want any request sent to mydomain.com/store/* to go through as there is another htaccess file in the store directory that does work there. That does not seem to be working at the moment. Any ideas?
Full htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
#ErrorDocument 404 http://www.mydomain.com/404Page.html
#Options +FollowSymlinks
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
RewriteCond %{REMOTE_ADDR} !^myip
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ http://www.mydomain.com/maintenance.html [R=307,L]
##301 Redirect Rules##
#some 301 redirects i did not include here
##Kohana Redirect Rules##
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system|kohana|vendors)\b.* http://www.mydomain.com/ [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?store/
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php [L]

Try this condition:
RewriteCond %{REQUEST_URI} !^/?store/
There is no need to check the characters after the directory. I made the first slash optional if I remeber correctly is the first slash only visible if your server configuration does not contain a tailing slash.

The issue turned out to be in the .htaccess file in the store directory not the one in the webroot. Thanks all, and sorry for my stupidity. IF anyone wants to leave comments on how to debug something of this nature for future users that would be awesome.

Related

point root to sub-directory with .htaccess

i have the rule to redirect all request to https and point to index.php
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:CF-Visitor} !{"scheme":"https"}
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Protocols h2 http/1.1
the root directory is htdocs, but i need change it to point to htdocs/public because some framework implement public folder has root. the problem is that i fin that example:
RewriteRule ^subfolder/$ /yourfile.php [L]
It's not clear to me how to implement it.
update:
my .htaccess file is in the root directory: htdocs, then index.php is located in htdocs/public, in this subdirectory I don't have the .htaccess
finally what I am looking for is that when entering a url like:
https://your-example.com/
https://www.your-example.com/
https://www.your-example.com/test-foo/
https://www.your-example.com/test-foo?data=data&foo=foo
all these requests point to index.php in the subdirectory:
htdocs/public/index.php
update 2:
my current routing in /public/.htaccess:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
the root directory is htdocs, but i need change it to point to htdocs/public because some framework implement public folder has root.
Ordinarily, if you have access to the server config then you would "simply" change the DocumentRoot in the server config to point to the /public subdirectory. Alternatively, if you have access to the directory above the document root (as you appear to) then move the file structure "up" a level, so that the files in the /public subdirectory are moved to the document root (ie. htdocs/) and the framework/system files are in a directory above the document root, outside of the public HTML space (as they should be).
However, if you don't have access to the server config and/or are unable to move the files, and you have a /public subdirectory off the document root then proceed as follows...
I assume your current HTTP to HTTPS rule/redirect is working OK for you (since this is specific to Cloudflare)? However, the rule you posted does not route anything to index.php as you appear to suggest and the Protocols directive is not permitted in .htaccess (and should be triggering an error) - so not sure what that is doing there?
You need to add a rewrite to the root .htaccess file (ie. /htdocs/.htaccess) that rewrites all requests to the /public subdirectory. And create an additional .htaccess at /htdocs/public/.htaccess that routes all requests to public/index.php (your front-controller).
For example, try it like this:
# htdocs/.htaccess
RewriteEngine On
# Redirect HTTP to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:CF-Visitor} !{"scheme":"https"}
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rewrite all requests to the "/public" subdirectory
# You could do this unconditionally, depending on your requirements.
# ie. You don't necessarily need to check for existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) public/$1 [L]
Also consider canonicalising the www vs non-www hostname (ie. redirect one to the other). This would go in the root .htaccess file before or after the HTTP to HTTPS redirect (depending on requirements, ie. are you implementing HSTS?).
Create a second .htaccess file at htdocs/public/.htaccess with the following:
# htdocs/public/.htaccess
DirectoryIndex index.php
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
# Route all requests to "index.php" (front-controller)
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
UPDATE:
I quick analysis of your existing rules, but as I mentioned in comments, the version I described above is preferable.
my current routing in /public/.htaccess:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
The RewriteBase / directive here is incorrect and will break the rules when located in the /public subdirectory. This would need to be either set to RewriteBase /public or removed altogether (preferable).
The QSA flag is not required since you are not including a query string in the substitution.
^(.*)$ - the capturing group is less efficient and not required (you are not making use of any backreferences here).
The <IfModule> wrapper is not required and should be removed. This would only be required if these directives are optional (they are not) and you are moving these directives to multiple servers where mod_rewrite might not be enabled.
The filesystem checks to make sure the request does not map to a directory (!-d) and is not a symbolic link (!-l) are generally not required (and consequently is an unnecessary overhead if they are not reqd). These should be removed, unless you specifically need to access filesystem directories (or symbolic links) directly (which is rare).

Need to remove a specific folder from .htaccess mod-rewrite redirect rules

I'm using some software that sits in the /var/www/html folder and manages URL redirects through .htaccess (the software isn't Wordpress, but it manages URL redirects in a similar way).
I need to carve two folders (/var/www/html/folder1 and /var/www/html/folder2) out from the redirect rules, which are at the very bottom of the .htaccess file. I read through a ton of documentation but can't seem to make the following work in .htaccess (which sits in /var/www/html):
Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
#force HTTPS
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#carve out these folders
RewriteRule ^/folder1($|/) - [L]
RewriteRule ^/folder2($|/) - [L]
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
When I go to the directories in question (eg. https://example.com/folder1), I get: Forbidden You don't have permission to access /folder1/ on this server.
Any ideas?
I can't comment as I'm new but do the two folders have any sort of index page? Otherwise you will get a 403 response because you have disabled indexes at the top of your file excerpt.

Redirect subfolder to the main domain (root) using mod_rewrite (htaccess)

So I bought a script from the other guy (nothing fancy, let's say just a customized CMS) which is kinda complicated and uses frameworks that I don't know nothing about except names like "bootstrap", "laravel" and so. I am the guy who knows the basis and know some things from intermediate level but again, only some so when I see some fancy solutions I am getting confused.
Like here - I have never seen page built on files with strange extensions and two .htaccess files - one in root, and second in /public/ folder. Still, even though there are so many files, everything works really fine and fast.
Here is the issue's description:
So it seems that when browser loads the page (domain.com), it requests (I guess) content from /public/ folder and everything works fine and domain remains as domain.com. The thing is, that domain.com/public also works and I want to create redirection on this specific address just to prevent indexing this crappy-looking address of domain.com/public but bearing in mind, that domain.com should still work fine.
I have tried maaany solutions found here on SO and on other pages but they resulted in either crashing page (internal server error) or not doing anything at all. I think some of them might work but only when files are not embedded in another sub-folder. Eh I don't know, I am out of ideas. Can you please help me?
Here is the root's .httaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
And here is the public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
It looks to me like what you have should work, specifically the first condition + rule in the root .htaccess is meant to do what you're asking. I would polish it up a little:
In /.htaccess:
RewriteEngine On
RewriteBase /
# Fix URLs that begin with "public" subdirectory
RewriteCond %{THE_REQUEST} \s/public(?:/(\S*))?\s
RewriteRule ^ %1 [NS,NE,R=301,END]
# Internally rewrite everything else
RewriteRule ^(?!public/).* public/$0 [DPI,L]
In /public/.htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Pretend this is the root
RewriteBase /
# Redirect trailing slashes if not a folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [DPI,R=301,END]
# Handle front controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!index\.php$). index.php [DPI,END]
</IfModule>
You can use L instead of END if your version of Apache is old, you'll know because of a 500 error.

.htaccess rewrite rule to make longer url work

I have a rewrite rule that allows me to be able to have a url like this..
http://example.com/randyt
and convert it to
http://example.com/?aff=randyt
It's working however, I also have other links that need to be used such as....
example.com/members/aff/go/randyt
example.com/members
example.com/members/signup
and so on. The rewrite rule I have ignores those urls and just goes to
example.com
Please help me add something to this so it will not ignore any url with the /members or /members/.... in it.
Here is the code I am using now...
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) ?aff=$1 [L,QSA]
Sorry about the comments section I am new here lol.
So here is the above code with the added code that someone suggested
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) ?aff=$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/members
RewriteRule . - [L]
When I try to use the extended url it still goes back to the base url showing only index.php from the root directory.
When someone uses a url like this....
example.com/members/aff/go/randyt
I need it to stay in the members directory using that affiliate code. The current program should take care of that.
Here is the .htaccess code in the members directory
<IfModule mod_rewrite.c>
RewriteEngine on
# RewriteBase /members
RewriteRule ^public public.php [L]
RewriteRule ^js.php js.php [L]
RewriteRule !\.(js|ico|gif|jpg|png|css|swf|csv|html)$ index.php
</IfModule>
<IfModule mod_php5.c>
# php_flag magic_quotes_gpc off
</IfModule>
That code was pre-written by the company I purchased the program from.
So to sum this up, I need 2 different things to happen...
If someone uses:
example.com/randyt (randyt can be any username)
it needs to stay on example.com showing that page (index.php file)
If someone uses:
example.com/members/aff/go/randyt (again randyt can be any username)
it needs to stay in the members directory and use the purchased script.
Try this rule in /members/.htaccess:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /members/
RewriteRule ^public/?$ public.php [L,NC]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
You can add a condition to your .htaccess
RewriteCond %{REQUEST_URI} ^/members
RewriteRule . - [L]
This condition will check if the Requested URI contains a string starting with /member then it will be passed unchanged to its destination.

Hide a folder from the URI with RewriteRule in a .htaccess file

I would really appreciate if you could help me building my .htaccess file.
What I want to achieve here is moving all of my domain's files and folders to a root subfolder ("mydomain") and make sure that the URL visible to my visitors still looks like
domain.com/file.extention
instead of
domain.com/mydomain/file.extention
or
domain.com/subfolder/file.extention
instead of
domain.com/mydomain/subfolder/file.extention
This is my folders tree:
/
/mydomain
/subfolder
index.php
test.php
and here is my current .htaccess file:
<IfModule mod_rewrite.c>
# System symbolic links are allowed.
Options +FollowSymlinks
# Runtime rewriting engine enabled.
RewriteEngine On
#
# BEGIN DOMAIN
#
# Make 'mydomain' subfolder the root folder for the domain.
RewriteCond %{HTTP_HOST} ^((www\.)?domain\.com) [NC]
RewriteRule ^\/?$ mydomain/ [NC,L,S=1]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?mydomain/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ mydomain/$1 [NC,L]
#
# END DOMAIN
#
</IfModule>
The problem here is that it works when I browse for
domain.com/subfolder/
but not with
domain.com/subfolder
and with
domain.com/index.php
but not with
domain.com/test.php
Thank you all,
Matteo
Changing domain.com/mydomain/file.extention to domain.com/file.extention
use this for example
RewriteRule ^mydomain/(.+)$ http://www.domain.com/ [R=301,L]
But if you redirects working just for some pages check that you clear browser cache or use another one instead