Directory Structure and Mod_Rewrite - mod-rewrite

I designed a php/mysql/query based on the idea of a subdirectory per function, so my directory structure looks like this:
public_html\
..users\ (all user related files are in this folder)
..events\ (all event related files are in this folder)
..groups\ (all group related files are in this folder)
..layouts\ (global header, global footer, and global navigation include files are in here)
Any pages in the users,events or groups folder include the header, footer and navigation files so they all keep a theme. Because of this the navigation file has all links set to
../functionfolder/file.php" and it works out because you are usually in one of the function folders.
But I forgot the index file in the public_html, when I include my headers, footers and nav from that directory (not being a sub folder) the "../"'s mess every thing up.
I have already created a separate navigation file that will make files in the high level directory work but I realized that all of my pictures and script includes are messed up.
(created a separate nav include with no ".."'s)
What I think may be a solution:
do not use a index.php file home director but create a directory named home and place the index file in there so everything will work out.
(public_html/home/index.php vs public_html/index.php)
(not sure how that would work out with the domain)
I have also looked at absolute vs relative links but they end up being a mess working on my computer then uploading them??
Anyone have an Ideas that can help?
Update:
would something like this work?
# Turn on rewrites.
RewriteEngine on
# Only apply to URLs on this domain
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Only apply to URLs that aren't already under folder.
RewriteCond %{REQUEST_URI} !^/folder/
# Don't apply to URLs that go to existing files or folders.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all those to insert /folder.
RewriteRule ^(.*)$ /folder/$1
# Also redirect the root folder.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ folder/index.php [L]

Related

.htaccess on filesystem not working

I tried using a .htaccess file on my computer (as in, not on an actual website), but it is not working. I looked for anything relevant, but all I could find were article discussing how Windows doesn't allow a file to be created with a dot as the beginning of the file name. I have made the .htaccess file, and am trying to remove the .html file endings from the URL:
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
However, when I typed the name of the file into the browser bar - without the .html extension - I received a 404 error. Why is the .htaccess not working on a file system, and how can I make it work?
You need to install a web server (e.g. Apache). The file system or web browser do not know how to interpret the .htaccess file, that is done by the web server.

Laravel pretty URL

I'm going crazy here! I'm trying to learn Laravel and pretty URLs just don't work.
I have enabled mod_rewrite from my apache config file, I have set AllowOverride to All in my user config file and I have the following in my .htaccess file in public folder of Laravel installation:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
I enter http://localhost/~user/lara/public/index.php/users it works, but with http://localhost/~user/lara/public/users I get 404 Not Found error
The requested URL /Users/user/Sites/lara/public/index.php/users was not found on this server.
You can see that the redirection works fine, as public/users have turned into public/index.php/users but it says not found, even though when I manually enter public/index.php/users it show me the output.
I have read all the related questions on SO, none of the worked for me!
This is most likely caused by the fact that you are changing the document root during a request. From the looks of your URL (with the ~user segment) you are using mod_userdir or something similar, and what these types of plugins do it allow you to map a given URL prefix to a document root that is not the normal one for the server.
As such, you sometimes run into issues like this where the right .htaccess file is found, but its rewritten URL is against the original document root rather than the modified one and so your index.php file cannot be found (maybe, to be honest I don't really know, this is all conjecture). This is also why going directly to index.php/users works - the problem isn't the setup per se, but the mixing of rewrite rules and the change of the document root.
As such, the fix is to use a RewriteBase line, ad the following to the .htccess file:
RewriteBase /~user/lara/public/

mod_rewrite to parent directory

I would like to create a .htaccess file that would do this logic:
If the requested file is not found try to find it in the directory
above.
I don't want to redirect the browser I would just like to internally rewrite the request.
I tried and searched for this a lot but always got stuck because (as I gather from the log) the where I could do the rewrite the path was always already without its per directory prefix. In the example below the .htaccess file is in the lang folder. If the lang specific file is not found it should just take the file from the parent folder. I understand that it is possible to do it by hardcoding the parent directory or by placing the .htaccess higher, but now that I suffered for so long in trying I would be very interested to learn if it was possible at all this way.
strip per-dir prefix: X:/localhost/htdocs/peopletest/public/img/root_cli/lang/en/loginhead.gif -> en/loginhead.gif
applying pattern 'somePattern' to uri 'en/loginhead.gif'
Thanks for the help.
SWK
Like this?
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-l
RewriteRule /[^/+]/([^/]+)$ $1

mod_rewrite positioning and wording

I renamed about 50 pages of my website. I want to make an internal rewrite from the old pages to the new pages. This is the example that Apache gives.
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo\.html$ bar.html
I am not sure if I need the rewriteBase /. I have only individual webpages (no subs).
I understand the terms "foo" and "bar" and "quux" are universal words for examples. If I have only one domain on this server, and the rewrite rule will apply to the root directory, do I need to include rewriteBase /, rewriteBase /~quux/, or do I even need rewriteBase?
I assume that when using rewriteBase /~quux/, the actual subdirectory is inserted were /~quux/ is. Even though I don't have subdirectories, is this correct?
Can someone please arrange the correct script illustrated above?
Also, I understand that this script would be placed BEFORE other .htaccess directives, such as non-www to www and index to /. Is this correct?
RewriteEngine on
Options +FollowSymLinks
#rewrite old to new pages internaly
RewriteBase /~quux/
RewriteRule ^foo\.html$ bar.html
#non-www to www
RewriteCond
RewriteRule ...
#index to /
RewriteCond
RewriteRule ...
RewriteBase:
If your page is like:
http://mydomain.com/subdir/index.html
and your .htaccess file is in subdir/, then you need to set it:
RewriteBase /subdir/
This lets you make your rules ignore the subdirectory, so
RewriteRule ^old_index.html$ new_index.html
would redirect subdir/old_index.html to subdir/new_index.html
Positioning:
The positioning of the rules only matter if you are not using the [L] flag after your rules. This modifier tells mod_rewrite to stop rewriting and make the redirect. Not using it will let the rewrite engine do everything it can with your url in one go. So if your url is like this:
http://mydomain.com/old_index.html
It will be converted to
http://www.mydomain.com/new_index.html
No matter which rule comes first, the one that adds the www. or the one that points to the new pages. But if there is an [L] flag, then it may be done in 2 redirects.

mod_rewrite preventing correct filetype icons from showing on mod_autoindex

I seem to have a problem which I've been struggling to solve. I'd love a bit of help here.
I have a dump site where I am using mod_autoindex and FancyIndexing.
I have my FancyIndexing icons defined in my .htaccess file. I also use mod_rewrite to pass the files through several php scripts (depending on the file extension).
Everything works great, except when a file extension is matched on a RewriteRule, Apache displays only it's default icon, and not the one specified.
All FancyIndexing resources are on the /imgindex directory, and all viewers are on the /viewers directory.
This is the relevant part of the .htaccess file (I'm currently tweaking it, so it's not optimized):
Options +Indexes
IndexOptions +XHTML +HTMLTable +FancyIndexing +FoldersFirst +SuppressHTMLPreamble +IconsAreLinks +IgnoreCase +NameWidth=*
IndexIgnore *~ imgindex viewers favicon.ico
HeaderName /imgindex/header.html
ReadmeName /imgindex/footer.html
# ------ Fancy Indexing ----------
AddIcon /imgindex/image.png .jpg .jp2 .jif .jpeg .tiff .tif .pict .pct .bmp .gif .png .psd .tga .ai .indd .fh* .fh10 .xcf .svg
AddIcon /imgindex/app.png .app
AddIcon /imgindex/movie.png .mov .mpg .mpeg .m2v .avi .divx .xvid .swf .wmv .wma .wm* .ram .rm .ogm .ogv
AddIcon /imgindex/txt.png .txt .text .log
# etc, etc.
DefaultIcon /imgindex/text.png
# ------ Rewriting --------
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^viewers/(.*)$
RewriteCond %{REQUEST_URI} !^imgindex/(.*)$
RewriteRule ^(.*).(css|cs|cpp|h|hpp|pas|xml|js|asm|inc|as|sh|bat|cmd|html)$ /viewers/view_source.php?file=$1.$2&%{QUERY_STRING} [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^viewers/(.*)$
RewriteCond %{REQUEST_URI} !^imgindex/(.*)$
RewriteRule ^(.*).(jpg)$ /viewers/viewjpg.php?file=$1.$2&%{QUERY_STRING} [NC,L]
# (etc, etc.)
As I said, everything seems to work great, except for the icons. In the case above, all .css, .cs, .cpp, (etc), and all .jpg files will use the icon defined in DefaultIcon, instead of the one for its filetype (defined in AddIcon).
If I remove the RewriteRule that matches that filetype, the correct icon gets displayed.
Is this normal, expected behaviour? Any way to override it, if so?
Regards,
Ok, seems I found out why. mod_rewrite is, for some reason (a bug?) remapping all apr_read_dir() calls, which mod_autoindex uses (I ended up looking at mod_autoindex source code). Since it's considered a subrequest on the mapper module, as a workaround I just added a:
RewriteCond %{IS_SUBREQ} false
To each of the RewriteRules and it magically worked. Of course, this is just a workaround, since if you actually need a rewriterule on a subrequest, this might not work for you.
I do believe this to be a bug on mod_autoindex, since there's absolutely no reason the filename for the icon and/or description should be ANY different than the one used to actually print the output data.
There could be some obscure reasons to it though.

Resources