Multiple Rewrite Rules, Try A then B - .htaccess

I have the following directory structure:
root
--/inc
--/img
--/docs
---/public
----/contact
-----/img
------telephone.jpg
-----contact.php
---/private
My aim is to make each folder under 'docs' a 'contained' webpage. Each folder will have it's own /img/ folder, and a /bin/ folder too, which could contain anything from Mp3s to PDFs.
Currently I am routing everything through to index.php, and then manually redirecting the file from there. But this is proving to be very slow. What I was thinking would be faster would be something like this in my .htaccess if say, an image was trying to be accessed via /contact/telephone.png:
try /img/{url_path}
Otherwise, try /docs/$1/img/$2
Otherwise route through index.php
How could I go about doing this? Currently my .htaccess is as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# if file not exists
RewriteCond %{REQUEST_FILENAME} !-f
# if dir not exists
RewriteCond %{REQUEST_FILENAME} !-d
# avoid 404s of missing assets in our script
#RewriteCond %{REQUEST_URI} !^.*\.(jpe?g|png|gif|css|js)$ [NC]
RewriteRule .* index.php [QSA,L]
</IfModule>
Any help appreciated! Thanks

You can do something like this in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# check if this image path exists in docs/<folder>/img first
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/docs/$1/img/$2 -f
RewriteRule ^([^/]+)/(.+?)/?$ /docs/$1/img/$2 [L]

Related

How do i use htaccess rules to rewrite to a certain file?

I have to create a page handler, which should read the URL, and do specific operations based on querystring.
I'd need to use .htaccess to do some URL rewriting thing to point everything at a certain file which does the processing, in such a fashion:
https://example.com/folder/page1/
https://example.com/folder/page2/
And the processing file is https://example.com/folder/index.php
Is there any way to do that (possibly by removing the index.php part)?
try this out and see if this helps you achieve what you are looking for.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
The above code is taken for reference from WordPress and it should work when the "RewriteBase /folder/" folder name is updated with your folder and .htaccess file has to be placed in the root/folder directory where your index.php file is located.

How to point all folders and files to root?

I want to point all subfolders and non-existent files to root folder.
www.domain.com/folder/david to www.domain.com/folder/
or
www.domain.com/folder/david/ to www.domain.com/folder/
You can try this in your /.htaccess file:
RewriteEngine on
# Make sure the requested path does not exist (file or folder):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If it doesn't exist, redirect it to `/folder/`
RewriteRule ^folder/(.*)/?$ /folder/? [NC,R,L]
The NC flag makes the rule case-insensitive. If you do not need it, you can remove it.
If you would like to make the redirect permanent, exchange the R flag for R=301.
If it doesn't matter whether or not it exists, use the following instead:
RewriteEngine on
# Redirect everything to `/folder/`
RewriteRule ^folder/(.*)/?$ /folder/? [NC,R,L]
Update
Based on your comments, it appears you are running an application in /folder/. As such, it would be better to place your .htaccess file in /folder/, and fill it with the following:
RewriteEngine on
RewriteBase /folder/
# Make sure the requested path does not exist (file or folder):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If it doesn't exist, rewrite it to index.php
RewriteRule ^ index.php [NC,L]
You then do not need an .htaccess file inside the root - only the one in /folder/ is needed.

CakePHP [.htaccess], static files as the root of project

I would like to be able to access static files at the root of my project, in a folder at the same level of app/. It's because that is the only directory to wich I have the permission to read-write files on my server, so our images are uploaded there.
So if someone writes this URL:
www.mysite.com/img-rw
It displays images in the folder at [project-root]/my-rw-dir
Any ideas on how to edit .htaccess files for something like this to be done?
Thanks
Taking into account that this is a course project (as the OP said in comments) and you are NOT concerned about security flaws, here is what you could do.
By default CakePHP has 3 nested .htaccess files:
/.htaccess (Project root folder)
/app/.htaccess
/app/webroot/.htaccess
The first (topmost) of them is the first to respond when you try to access www.mysite.com:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Clearly, this sends the user request right into the /app/webroot folder. We could use the conditional check that exists in /app/webroot/.htacess to, instead of promptly redirecting to webroot, check if the given URL matches to a file or folder. In order to do so, you'll need to update the parent (/.htaccess) file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d # Checks if the given URL matches to a folder
RewriteCond %{REQUEST_FILENAME} !-f # Checks if the given URL matches to a file
#RewriteRule ^$ app/webroot/ [L] # This line is commented, should be removed
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
This way, the request will only be redirected to /app/webroot if none of the conditions are met. If you need only to return files, than you could remove RewriteCond %{REQUEST_FILENAME} !-d, which verifies if the URL matches to a folder ( -d = directory).
A cleaner version that would only check for files should look like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
With this tweak, accessing www.mysite.com/image.jpg should return the file image.jpg hosted at your project root (/image.jpg).
For anyone wondering, this works great :
.htaccess (project root)
<IfModule mod_rewrite.c>
RewriteEngine on
# This folder is a subfolder of project root but contains static images
# Accessed like so: www.mysite.com/imgrw/[file name]
RewriteRule ^imgrw/(.*)$ /dir-at-root-level/img/$1 [L]
# Everything else is handled by cakephp webroot
RewriteCond %{REQUEST_URI} !(dir-at-root-level) [NC]
RewriteRule ^(.*) app/webroot/$1 [L]
</IfModule>

htaccess and rewriting urls

currently i have a /en/ folder that is empty except for a .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ ../index.php?language=en$1 [NC]
i use it to eliminate the need for
index.php?language=en
in all my URLs. I would like to modify the htaccess in a way that i no longer need the /en/ folder with nothing but the htaccess inside. ideally i would like an htaccess in my root folder that reads the url and if it is www.example.com/en/ to rewrite to www.example.com/index.php?language=en
This should work for you:
RewriteRule ^en/(.*)$ index.php?language=en$1 [NC]
Put the following code in .htaccess file in your root folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)$ index.php?language=en$1 [L]
Here $1 will append rest of the url as well. The condition will also help if you request your files using direct url.

htaccess help needed

i'm doing maintenance work on a cms and have found the following htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
i'm having trouble understanding it.
the reason a went looking for the htaccess file is this:
i placed some code in index.php (right now just printing some string to a file but
eventually will do banner cycling) and i've noticed the string gets printed a few times when i load index.php. could that have some connection to the htaccess file?
thanx in advance for any input.
This simply checks whether a file exists (as a file -f, or directory -d). If it does not, it takes the address and passes it to index.php.
For instance if you ask for:
www.mysite.com/badfile.html
You will get:
www.mysite.com/index.php/badfile.html
This should have no effect on how the code in index.php runs. This only affects what happens when non-existent files and directories are requested.
Request is for a file on the webserver, denies access to index.php
RewriteCond %{REQUEST_FILENAME} !-f
Request is for a physical directory on the webserver, denies access to index.php
RewriteCond %{REQUEST_FILENAME} !-d
Any other than the above, redirects to index.php
RewriteRule ^(.*)$ index.php/$1 [L]

Resources