I have the following structure
index.php
.htaccess
system
- config
- classes
- modules
- module_name
- js
- css
My goal is to disable direct access to whole "system" folder excluding asset folders "js" and "css"
How to do that properly using RewriteRule in the root .htaccess?
Currently I figured out how to disable "system" directory using RewriteRule ^system/ - [F]
It works fine but also disables my assets.
You need to include your more specific exceptions before your block-all rule. For example, try the following:
# Exceptions: Don't block these URLs
RewriteRule ^system/modules/module_name/(js|css)/ - [L]
# Block the rest...
RewriteRule ^system/ - [F]
The preceding exceptions simply prevent any further rewriting when URLs of this format are requested.
UPDATE: If the modules and modules_name directories are variable then you can change this to:
RewriteRule ^system/\w+/\w+/(js|css)/ - [L]
\w being the shorthand character class for [A-Za-z0-9_].
Related
I'm trying to allow access to a folder called .well-known but I've found this rule that blocks hidden directories;
# block hidden directories
RewriteRule "(^|/)\." - [F]
Obviously there's a reason why this was added (I inherited the code) but I was wondering if I could keep this rule but add an exception for the .well-known folder?
You can indeed! You need to include a Rewrite Condition:
RewriteCond %{REQUEST_URI} !^/\.well-known
Put this before the actual RewriteRule, this condition basically tells the server NOT to hide the .well-known folder.
Make sure you clear your cache before testing this.
I have two potential paths that could be used:
www.example.com/1/a
www.example.com/1/a/this-is-a-title
I want to serve both urls to the same php file. I need the title to be an optional GET field that can either be supplied or not.
RewriteRule ^1/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ links.php?link=$1&title=$2 [PT,L,QSA]
RewriteRule ^1/([A-Za-z0-9-]+)/?$ links.php?link=$1 [PT,L,QSA]
For some reason the above does not work. It's not serving either of the path options.
What am I getting wrong here?
For my family members I was giving each person their own subdomain
(sister1.mydomain.com, sister2.mydomain.com, etc...)
I was using PHP to detect the domain, and then I'd load information related to the subdomain dynamically.
I'd like to get rid of the subdomains and use the power of .htaccess
My goal is to give the same URL:
www.mydomain.com/sister1
www.mydomain.com/sister2
www.mydomain.com/mommy
www.mydomain.com/daddyo
Obviously, I don't plan to have literal working directories for each person.
I'd pass the "sister1" portion to a process.php script that takes care of the rest.
I've figure out how to do it by manually typing each RewriteRule in my htaccess file:
Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteRule ^/?sister1$ process.php?entity=sister1 [L]
RewriteRule ^/?sister2$ process.php?entity=sister2[L]
RewriteRule ^/?mommy$ process.php?entity=mommy[L]
RewriteRule ^/?daddyo$ process.php?entity=daddyo[L]
I feel this is the long way of doing it.
Is there a more universal way of extracting the text after the first "/" forwardslash, and passing it to process.php?entity=$1 ?
I tried it this way:
RewriteRule ^/([A-Za-z0-9-]+)/?$ process.php?entity=$1 [NC,L]
I'm getting the apache 404 error: "Not Found".
It is because you have a mandatory / in the beginning of your rule, i.e., you are always looking for something like /sibling in the URL. Your first examples have that first forward slash as optional due to the question mark after it.
You do not need a beginning forward slash - normally the rewrite rule picks up stuff after the domain name
www.example.com/string/mod/rewrite/gets/is.here
So just remove the starting slash and it should work.
I have an Apache server, hosting a number of websites for my company. I plan to use htaccess, and rewrite the URLs from the "root" directory to a subfolder.
Here is my real folder structure:
/www (root)
/www/beamstyle (beamstyle.com.hk, CodeIgniter framework)
/www/beamcard (beamcard.com.hk, Static files)
/www/beamcard/app (beamcard.com.hk/app, CodeIgniter framework)
====================================================================
The beamstyle website works using the following code:
ReWriteCond %{HTTP_HOST} ^(www.)?beamstyle.com.hk [NC]
RewriteCond %{REQUEST_URI} !^/beamstyle/.*$
RewriteRule ^(.*)$ /beamstyle/$1 [L]
The above works, because my framework (CodeIgniter) is inside /www/beamstyle.
Therefore, I can access http://beamstyle.com.hk, and it will get redirected without any issues.
====================================================================
However, here is the problem. When I do the beamcard website, it doesn't work because actually, the directory "/www/beamcard/" actually stores ONLY static .html files. My CodeIgniter framework is inside app/.
When I use the same code:
ReWriteCond %{HTTP_HOST} ^(www.)?beamcard.com.hk [NC]
RewriteCond %{REQUEST_URI} !^/beamcard/.*$
RewriteRule ^(.*)$ /beamcard/$1 [L]
Here are the results using the above code:
(a) http://beamcard.com.hk/ <-- OK no problem, because this is the immediate folder after rewrite (and contain static files only)
(b) http://beamcard.com.hk/app <-- NOT OK, because it steps one directory beyond the immediate folder after rewrite.
If I type this, the location bar (on the top) gets un-disguised and redirected to http://beamcard.com.hk/beamcard/app/ (I confirmed that this redirect not done by Codeigniter files because the same result happens when I apply on an empty directory)
====================================================================
I've tried everything I could, and did so many Google searches, but failed to fine an htaccess snippet that works on subdirectories BEYOND the directory after rewrite. It loses it's "disguise" functionality when I step further in the subdirectories.
Any help on this is greatly appreciated!
Cheers,
Thomas
====================================================================
Update 1
In order to describe my situation better, I've put together some use cases.
[ Case Scenarios ]
** To simplify things, let's assume the "app" directory is empty **
(1) If I type in http://beamcard.com.hk, the page "/www/beamcard/index.html" inside the server should get loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/".
(2) If I type in http://beamcard.com.hk/contact_us.html, the page "/www/beamcard/contact_us.html" inside the server should get loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/contact_us.html".
(3) If I type in http://beamcard.com.hk/app, an "empty file listing" should be loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/app/".
(4) If I type in http://beamcard.com.hk/app/, an "empty file listing" should be loaded. After finish loading the page, the location bar should write "http://beamcard.com.hk/app/".
========================================================================
Currently, (1) and (2) works.
However, for (3) and (4), after finish loading the page, the location bar got redirected to "http://beamcard.com.hk/beamcard/app/", which "reveals" the "/beamcard/" portion, which ideally should be hidden from the site visitor.
Try adding NS at the end (to prevent the rule from being applied to sub-requests):
ReWriteCond %{HTTP_HOST} ^(www.)?beamcard.com.hk [NC]
RewriteCond %{REQUEST_URI} !^/beamcard/.*$
RewriteRule ^(.*)$ /beamcard/$1 [L,NS]
I have a directory structure with the following on localhost:
http://localhost/testing/
A directory structure exists inside of testing as follows:
/testing/public
/testing/public/index.php
/testing/public/img
/testing/public/css
..etc for the js and swf directories
A .htaccess file is inside the testing folder and the contents are as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /testing/
RewriteRule ^public$ public/ [R,QSA]
RewriteRule ^public/$ public/index.php?state=public [L,QSA]
RewriteRule ^stackoverflow$ stackoverflow/ [R,QSA]
RewriteRule ^stackoverflow/$ public/index.php?state=stackoverflow[L,QSA]
I am using PHP and inside of the /testing/public/index.php file I wanted to test that the $_GET['state'] is indeed saving the variable.
When I try to test out:
http://localhost/testing/public
$_GET['state'] is not found at all BUT
http://localhost/testing/stackoverflow
does indeed echo out that $_GET['state'] equals 'stackoverflow'.
What am I missing here??? Why is it that I cannot get the state=public in the first link? Thanks for the help!
This works fine on my system, but I think you are getting into trouble by having a rewrite rule with the same name as an actual file system directory. The file-system will generally take precedence. So when you load up '/testing/public' it just loads /testing/public/index.php without running your rule at all.
Try changing the rule to this:
RewriteRule ^public_$ public_/ [R,QSA]
RewriteRule ^public_/$ public/index.php?state=public [L,QSA]
Navigate to 'testing/public_', if that prints out 'public' as expected then you will know that was your problem.