htaccess rewrite / remove parent dir - .htaccess

there's been similar posts about this but I can't quite seem to find what I need.
I want my .htacess to rewrite "up one level".
The Url would be somethign like
http://www.site.com/variable_dir/
or
http://www.site.com/variable_dir/sub_dir
I need that to basically rewrite the request to
http://www.site.com/
or
http://www.site.com/sub_dir
I DO want the URL to still show the original
http://www.site.com/variable_dir/
or
http://www.site.com/variable_dir/sub_dir
I currently have
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
This redirects to where I want, but this changes the URL to
http://www.site.com/
or
http://www.site.com/sub_dir
which I don't want.
I know it's simple but I just can't seem to get there.

The rule below woule rewrite http://www.site.com/variable_dir/ to http://www.site.com/ and http://www.site.com/variable_dir/sub_dir to http://www.site.com/sub_dir
RewriteEngine on
RewriteBase /
#for a request to /variable_dir
RewriteCond %{REQUEST_URI} ^/variable_dir/(.+)$
#rewrite it to directory without variable_dir
RewriteRule . /%1 [L]
Edit:
If the directory is not literally variable_dir, the rule above will not work. However, if you have a short list of directories, you could enumerate them as below.
RewriteEngine on
RewriteBase /
#only apply if this directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
#for any direcory enumerated here
RewriteCond %{REQUEST_URI} ^/(variable_dir|dir2|dir3|etc)/(.+)$
#rewrite it to directory without variable_dir
RewriteRule . /%2 [L]
If not, then ideally the directories would all have something in common so you could limit what the rule affects. If you want a completely variable dir, nothing in common, I don't recommend it, but you can try
RewriteEngine on
RewriteBase /
#only apply if this directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
#skip any top level directory
RewriteCond %{REQUEST_URI} ^/[^/]+/(.+)$
#rewrite it to directory without variable_dir
RewriteRule . /%1 [L]
Edit:
Finally if the trailing slash is optional, as in the example in your comment, change the RewriteCond and rule above to be
#skip any top level directory, optional trailing slash
RewriteCond %{REQUEST_URI} ^/[^/]+(/(.+))?$
#rewrite it to directory without variable_dir
RewriteRule . /%2 [L]

Related

htaccess - can't load files directly any more

I goit this htaccess:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?p=$1&title=$2 [L]
So I can rewrite example.com/123/hey to example.com/?p=123&title=hey
But now I can't call files like example.com/pics/jo.jpg directly any more
Is there any way to call the files directly and keep the rewrite rules by modifying the htaccess or something?
The reason why you can't call your /pics/jo.jpg is because your your pattern ^([^/]*)/([^/]*)$ matches all requests of this form /foo/bar , so when /foo/bar.jpg is requested your rule rewrites it to /?p=foo&title=bar.jpg . To solve this ,you need to exclude your existent files and dirs from the rule using RewriteConds
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /?p=$1&title=$2 [L]

How to add an excluding rewrite (un-rewrite) condition for specific file types?

I have the following simple rewrite, which I am taking a slightly different approach to rewriting site content.
RewriteEngine On
RewriteRule ^(.*)$ controller.php?page=$1 [NC,L,QSA]
The goal, is to rewrite all files and folders, everywhere, except if the file is of a particular type.
Traditionally, the following approaches are taken which are bit too relaxed for this endeavor :
1. Exclude all files/folders that physically exist:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
2. Exclude specific folders from rewrite
RewriteRule ^somepath - [L]
3. Rewrite only specific file types
RewriteRule ^\.html index.php [NC,L,QSA]
4. Combination of #2 and file types
RewriteCond %{REQUEST_URI} !/images/.*
RewriteRule ^(.*\.(gif|jpg|png))$ images/$1 [QSA,L]
What I am wondering is what rule do I insert to exclude (keeping the sample short): jpg,bmp,png from being rewritten to controller.php regardless of the subfolder they are located in.
Pseudocode
RewriteEngine On
# skip rewriting jpg,bmp,png
RewriteRule ^[..something here..] - [NC,L]
# rewrite everything else
RewriteRule ^(.*)$ controller.php?page=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !.*\.extensiongoeshere

URL rewrite checks for static files then rewrites to index.php

OK, I'm pants at rewrite rules in .htaccess files!
My desired scenario is (using the URL http://doma.in/ as an example):
First check to see if an index.html file exists in the /public sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php
To expand on my example, say we requested the URL http://doma.in/js/foobar.js:
First check to see if an foobar.js file exists in the /public/js sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php?controller=js%2Ffoobar.js
That would cover static files but I also need URLs like http://doma.in/foo:
First check to see if an foo file exists in the /public sub-dir; if it does, serve it
If it did not; serve (rewrite to) index.php?controller=foo
And a URL http://doma.in/foo/bar:
We can assume the file foo/bar does not exists in the /public sub-dir as files can't be named like that.
So serve (rewrite to) index.php?controller=foo&action=bar
I'm sure if this complicated (for me) bit is covered then I can work query-strings into the occasion too; so http://doma.in/foo/bar/foo/bar would serve index.php?controller=foo&action=bar&querystring=foo%2Fbar.
I'd also like to make sure that a trailing slash is handled the same as if a trailing slash was omitted, for example: http://doma.in/foo/bar/foo/bar and http://doma.in/foo/bar/foo/bar/
I'll handle 404s from within my app as if the file did not exist, it would redirect to index.php which does exist - I'm happy with this unless you've a better solution :)
I really hope all this makes sense as I've been looking to find a solution to this scenario all day now and this is all I have:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteBase /prompt-v3/
#RewriteCond %{REQUEST_URI} ^/prompt-v3/(.*)$
RewriteCond $1 !^public
RewriteRule ^(.*)$ public/$1 [R]
The commented-out lines deal with a sub-dir when on a remote host. So far I can redirect to the /public sub-dir if the file exists there and that's about it.
Thank you everyone for your help!
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteBase /sub-dir/
#RewriteCond %{REQUEST_URI} ^/sub-dir/(.*)$
RewriteRule ^index.html$ $1 [L,R=301]
RewriteCond $1 !^public
RewriteCond $1 !^lib
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond $1 ^public/$
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ lib/bootstrap.php [L]
RewriteCond $1 !^public/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 ^public/(.*)$
RewriteRule ^(.*)$ lib/bootstrap.php?path=%1 [L]
This will look for an index.html file in the public directory and if it does not exist rewrite the URL to lib/bootstrap.php. it in fact checks for any request as a static file in the public directory first and deals with canonicalisation too.
Put this .htaccess in your document root
<ifModule mod_rewrite.c>
RewriteEngine on
# For /js/foobar.js (/js/*)
RewriteRule ^js/(.*)$ /public/js/$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/(.*)$ index.php?controller=$1 [NC,L]
# For foo/bar (/*/*?*)
RewriteRule ^(.*)/(.*)$ /public/$1/$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^public/(.*)/(.*)$ index.php?controller=$1&action=$2&querystring=%{QUERY_STRING} [NC,L]
</IfModule>

Rewrite rule to hide folder, doesn't work right without trailing slash

i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that sub-directory. I found several quite similar issues on stackoverflow, but nothing really fits, so i decided to post a new question.
My .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ foo/$1 [QSA,L]
The document-root only contains the following folder/files:
/foo/bar/index.html
I would now expect that example.com/bar and example.com/bar/ would just show me the contents of index.html.
Instead example.com/bar/ show me the content as expected but example.com/bar redirects me with a 301 to example.com/bar/foo/ an then shows the contents. I really don't get why there is a 301 redirect in this case.
When i put something this
RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteCond %{REQUEST_URI} !^[^.]*\.html$
RewriteCond %{REQUEST_URI} !^[^.]*\.php$
RewriteRule ^(.*)$ $1/ [QSA,L]
on top of that rule it seems to work, but that would require me to list every used file extension...
Is there any other way i can omit the redirect, the folder "bar" should never be seen by an outside user.
Thanks in advance!
1st rewrite rule is redirect from /foo/(.) to ($1) and second - from (.) to $1.
just idea, this has not been tested.
Better late than never...
Got it working with a simple RewriteRule which append a / to every url that doesn't have on.
# only directories
RewriteCond %{REQUEST_FILENAME} !-f
# exclude there directories
RewriteCond %{REQUEST_URI} !^/excluded-dirs
# exclude these extensions
RewriteCond %{REQUEST_URI} !\.excluded-extension$
# exclude request that already have a /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]

Logical AND in htaccess modrewrite?

This is my htaccess file
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/chat/
RewriteCond %{REQUEST_URI} !^/m/
RewriteCond %{REQUEST_URI} !^/__admin/
RewriteCond %{REQUEST_URI} !^/gzip_headers.php
RewriteCond %{REQUEST_URI} !^/phpfreechat/
RewriteCond %{REQUEST_URI} !^/_temp/
RewriteRule ^.+\.php$ index.php [L]
RewriteRule ^.*\.css gzip_headers.php [L]
RewriteRule ^.*\.js gzip_headers.php [L]
RewriteRule ^classifieds/ /index.php [L]
RewriteCond %{REQUEST_URI} !^/movies/.
RewriteRule ^movies/ /index.php [L]
RewriteCond %{REQUEST_URI} !^/games/.
RewriteRule ^games/ /index.php [L]
RewriteRule ^jntu/ /index.php [L]
RewriteRule ^news/ /index.php [L]
My idea behind this basically is,
forward everything to public_html/index.php (except some directories)
forward all js and css to gzip file, ( i am doing this basically because im not jsut gzipping them but also compressing in tha phpfile)
the problem is when I load images from subdirectories the are redirected to index.php as well, so just creating conditions for those directories and storing images in them like RewriteCond %{REQUEST_URI} !^/games/.
I would like to make it simple to do stuff like this
forward everything to index.php (except some conditions on top)
forward css and js to gzip file
load images and flash and some other mime types straight away only if they exists. (jpg|gif|png|swf|flv|mp4|3gp|mp3|zip|rar|exe)
Something like logical AND REQUEST_URI and -f flag I guess
Try these rules:
RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteRule .*\.(js|css)$ gzip_headers.php [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .+\.(jpg|gif|png|swf|flv|mp4|3gp|mp3|zip|rar|exe)$ - [L]
RewriteCond %{REQUEST_URI} !^/(gzip_headers|index)\.php$
RewriteCond %{REQUEST_URI} !^/(chat|m|__admin|phpfreechat|_temp)/
RewriteRule ^.+\.php$ index.php [L]
I'm not sure why your images are being redirected if your rule only redirects URIs ending with '.php'. That should exclude all other file extensions from the rule.
I'm also not sure what you mean by needing 'logical and'. When you have a number of RewriteCond lines before a RewriteRule those conditions are ANDed together and the rule is only applied if they all are true.
You can't use modrewrite to check for the existance of files and say "if the file exists, don't apply any rules, just serve up the file".
I think the best solution would be to either use a single top-level directory called 'static' or 'images' where you put all your files and exclude it from the rules, or have a wider-matching rule.
So for example you could make 'static' or 'images' a special directory name and exclude any url that contains .*/images/.* from the rules. Then /something/images/image.jpg and /something/else/images/image.jpg would both be excluded and the file would be served up.
Another hacky way would be to serve the files up from PHP. So in PHP you would translate $_SERVER['REQUEST_URI'] into a filename and see if it exists. If it does, you can write the file contents to the PHP output stream, although this won't be as efficient as leaving it up to Apache, and actually I really would not recommend it.
But like I said before, if your rule is only matching files that end with .php then your images should not be getting redirected. I would figure out why this is happening first. There is a way to turn on debug logging for mod_rewrite but you'll have to Google that.

Resources