htaccess do not allow to access a file - .htaccess

This is my file structure. I need to get access to the images folder through a URL and get images. But I can`t access that folder. This is the URL that I need to get access. http://10.0.2.2/careuAppWeb/careu-php/images. I need to get pictures in the images folder and display it somewhere else.Is there any way that I can get access to that folder and get those images. I think the problem is my .htaccess file.
This is it.
Options -Indexes
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
How can I get access to Images folder.

RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
You can change your mod_rewrite directives to something like the following instead:
RewriteCond $1 !^images
RewriteRule (.*) public/$1 [L]
This creates an exception for any URL to the /images subdirectory and rewrites everything else to the /public subdirectory.
The first RewriteRule you had was superfluous and is handled by the 2nd rule anyway.
This does assume you have an additional .htaccess file in the /public subdirectory that also contains mod_rewrite directives, otherwise you get a rewrite-loop.

Related

User friendly URL through .htaccess

My url looks like -
http://localhost/user_notes/?id=1234.
I want to be turn it into user friendly url like
http://localhost/user_notes/1234
My .htaccess file looks like -
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
RewriteRule ^(.+)$ /user_notes/?id=$1
</IfModule>
The problem is with RewriteRule ^(.+)$ /user_notes/?id=$1
For more info see my directory structure
Note : In my directory structure there is also a public folder. But I can access files without including public in URL through htaccess file (line 3).
Could you please place these rules at top of your htaccess file(I hope there are NO redirect rules for https in your htaccess file). Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^ public%{REQUEST_URI} [L]
RewriteRule ^(user_notes)/(.*)/?$ $1/?id=$2 [NC,L]

modify .htaccess rewrite rule

I am new to web development. In my site I have public_html folder and it has the app folder. I have following RewriteRule in my htaccess file :
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
Now I want to modify the rule such a way that if I create new folder profile under public_html than it should display the index.php of the profile folder. Basically, If I type www.example.com/profile in browser window, it should display the content of index.php file.
There are a few ways you could do this, but this should be the simplest way for you.
RewriteRule ^[a-z0-9_-]+/?$ ./profile/index.php [L,NC]

.htaccess Rewrite redirecting URLs

In my .htaccess file, I've got the fairly standard
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1 [NC]
and I only need this to happen to files in my root directory. However, if I go to the URL of one of my subdirectories (with or without a index.php file), such as www.foo.com/bar, then I am redirected to www.foo.com/bar/?p=bar - how do I prevent the addition of ?p=bar?
You can try making the rule only execute for non directories as below
#if it is not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#then send it to index.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1 [NC,L]
You can put .htaccess files into the sub-directories.
# /bar/.htaccess
AllowOverride none
This should prevent the .htaccess inheritance.

htaccess RewriteRule redirecting to parent directory?

I cant understand how to redirect to the parent directory in this case:
I have a root directory with .htaccess file, where rewriteEngine is on. In this directory I have a file rootFile.php.
Also, in this directory I have a directory "forum", with another .htaccess file in it.
When there is request to "forum.mySite.com/rootFile.php", I need to redirect it to "mySite.com/rootFile.php" which means I need to redirect request to the parent directory.
/forum/.htaccess:
RewriteEngine on
RewriteBase /forum/
RewriteRule ^sitemap /forum/map.php [L]
...
I tried to:
RewriteEngine on
RewriteBase /forum/
RewriteRule ^rootFileName ../rootFile.php [L]
\# or so RewriteRule ^rootFileName /rootFile.php [L]
\# or so RewriteRule ^rootFileName rootFile.php [L]
RewriteRule ^sitemap /forum/map.php [L]
Also tried this one:
RewriteEngine on
RewriteBase /
RewriteRule ^rootFileName ../rootFile.php [L]
\# or so RewriteRule ^rootFileName /rootFile.php [L]
\# or so RewriteRule ^rootFileName rootFile.php [L]
RewriteBase /forum/
RewriteRule ^sitemap /forum/map.php [L]
And it always redirect from /forum/rootFileName to /forum/rootFile, but never to /rootFile.
Where is my mistake?
You can't rewrite to outside the document-root. This is a security thing.
You could just create a rootFile.php that only contains <?php include('../rootfile.php'); ?>
You can't rewrite to paths that are outside your document root for security reasons. In fact this should be avoided because it is not a good practice.
Anyway, to answer your question, a workaround might be creating a symlink in your document root that points to the target/parent directory, but you should protect it from direct accesses.
Let me do an example. Assume that our document root is /var/www/project1 and that you want to rewrite on /var/www/project2/target.php
Then, you have to create a symlink inside /var/www/project1 that points to /var/www/project2 named (for example) p2.
This should be your /var/www/project1/.htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /hello/world
RewriteRule ^(.*)$ p2/target.php [QSA,L] # p2 is the symlink name!
</IfModule>
You wrote:
When there is request to "forum.mySite.com/rootFile.php", I need to
redirect it to "mySite.com/rootFile.php" which means I need to
redirect request to the parent directory.
Did you mean:
When there is request to "mySite.com/forum/rootFile.php", I need to
redirect it to "mySite.com/rootFile.php" which means I need to
redirect request to the parent directory.
...Which in turn means:
When there is request to "/forum/rootFile.php", I need to
redirect it to "/rootFile.php" which means I need to
redirect request to the parent directory.
In your .htaccess in the /forum dir try this:
RewriteEngine On
RewriteRule ^rootFileName(.*) /rootFile.php [QSA,R=301,L]

htaccess redirection exceptions

i have the following .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?type=Navigation&action=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?type=Navigation&action=$1
Options All -Indexes
which sends everything to index.php in the root folder. But in my structure i have something like
/root
/.htaccess
/index.php
/files
/intranet
i was wondering if there is a RewriteRule exception so that when i type www.mysite.com/intranet it goes to the 'intranet' folder without passing trough the index.php file
thanks!
Yes. I believe you just put a rule before the others with the url you do not want rewritten, and specify that it shouldn't be rewritten using - like so:
RewriteRule ^intranet/.*$ - [L]
RewriteCond %{REQUEST_URI} ^\/intranet\/?
RewriteRule ^([a-zA-Z0-9_-]+)\/?$ index.php?type=Navigation&action=$1 [L]
Will do the trick.

Resources