Within Apache, how can I call the default directory index creator on-demand?
Here's the situation. Requirement is to display, say, a default image file for a URL that corresponds to a directory. That's easy to do, using mod_dir. But a further requirement is that, when the image is clicked (in the browser), display a directory listing. Now I realize that can be done by writing some directory-listing code (in PHP, say). But that seems like re-inventing the wheel. So ideally I'd like to invoke the code in mod_autoindex directly.
This may well not be possible at all.
One possible, untested approach that may be worth a try:
Set up DirectoryIndex as you have it now
Build a conditional URL rewrite that is fired on the directory index, but only if a certain GET parameter - let’s say showlist- is not set to yes. For how to write a RewriteCond that checks for GET parameters in URLs, see this question. To address only directories, use
RewriteCond %{REQUEST_FILENAME} !-d
If that condition is not met, have RewriteRule redirect to the page with your image. That way, when I open a directory on your server, I will be taken to that page.
On the page with the image, have a link to the directory with the GET parameter specified:
http://my.domain/directoryname/?showlist=yes
That should not redirect back to the file with the image, but display Apache’s directory listing.
Related
I have a url similar to:
https://example.com/event/FI42382
I have created a folder named event from which I want to use PHP to process the FI42382 part.
However, the last part is treated as its own directory. What do I need to do so that it is not a directory and instead I can manipulate the last path as a variable in event/index.php?
Sorry if I have worded the title poorly. I wasn't sure how to explain this.
You need to pass value which you want to access through a query string to your index.php etc. In your .htaccess file try with following Rules once. Also make sure place your .htaccess file in root(same level along with your event folder is present, NOT inside event folder please). Later in your php code you could get/extract the var value which is passed to it.
Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule %{QUERY_STRING} ^$
RewriteRule ^event/(.*)/?$ event/index.php?var=$1 [NC,L]
We are running a Sinatra app that caches some static pages to a cache folder and we would like to serve these files with Apache rather than Rack.
This is our proposed user flow (for example):
User visits http://domain.com/dir1/dir2 (eg: http://domain.com/archive/books)
Sinatra's router checks if an up to date cached version exists, if not, render out the file and save to public/cache/archive/books.html
Redirect the user to the appropriate location so Apache can serve the static file from http://domain.com/cache/archive/books.html but display the URL as http://domain.com/archive/books
We are trying to use a simple mod_rewrite rule to achieve this:
RewriteRule ^(.*)$ cache/$1.html
But can never seem to get it to work (throws a 500 server error).
I suppose an initial and perhaps more important question would be: Is this the right way to go about this? We are using Sinatra Cache, and I assume it generates the files but doesn't handle the routing to serve a cached file if it exists? (The gem is generating the static pages for us, but our server is always serving a dynamically generated file, not a cached static one)
This gem also hasn't been updated in nearly 5 years so we're unsure if this is even a recommended path to go down in any case (It seems to generate the static files we need quite well)
You can try this rule:
# First make sure .html file exists in cache before rewriting
RewriteCond %{DOCUMENT_ROOT}/cache/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ cache/$1.html [L]
It's a little difficult for me to ask the question correctly in title, but mainly here's what I'm trying to figure out.
If I use .htaccess on my website and say I'm trying to create a specific path "redirect" so that anything inside that path uses it's non-redirected path for it's relative path. Sorry for the structure of that sentence.
So for example...
I have my website as-
http://www.example.com/
I want to keep things organized so I create a sub-folder/subdirectory to keep all these folders inside of.
http://www.example.com/projects/
So the folder projects will be the place holder for all future projects and I want to create a folder inside projects for each project, so that I can define a URL for each one like such...
http://www.example.com/SuperFish/
http://www.example.com/AquaFear/
Now the folder for SuperFish and AquaFear would be under http://www.example.com/projects/ but also in their own folder say...
http://www.example.com/projects/01/
http://www.example.com/projects/02/
Now it'd be fine and dandy to keep the links like such, but for memory purposes and to share with other people the links to those projects, I'd like to just create a nice simpler URL with a custom "name". like http://www.example.com/SuperFish/ instead of http://www.example.com/projects/01/.
Now I was able to do this partially with my .htaccess file in the root folder with this code...
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^SuperFish/?$ projects/01/ [NC,L]
RewriteRule ^AquaFear/?$ projects/02/ [NC,L]
The issue with this though is that now I can't use relative paths with my SuperFish or AquaFear made up URL links. Anything such as .css, .js, .etc... Isn't within scope because it's trying to look for those files through the made up URL rather than the real physical one.
So the question is, how would I make it so that I can use my made up URL and also have the webpage load things to the real physical path or somehow fix the relative paths by doing this within the .htaccess file?
The reason I titled this the way it is, is due to subdomains being able to find their files with relative paths even though they're a subdirectory just the same.
Using final $ blocks files inside this two folders from being redirect correctly. You could tried instead :
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^SuperFish/?(.*)$ projects/01/$1 [NC,L]
RewriteRule ^AquaFear/?(.*)$ projects/02/$1 [NC,L]
I have the following rules in .htaccess file placed at the root folder
RewriteEngine on
RewriteRule ^/?$ /testsite/index.php?rel=m
RewriteRule ^about/?$ /testsite/aboutus.php?rel=b
RewriteRule ^ict/?$ /testsite/ict.php?rel=b
RewriteRule ^eeap/?$ /testsite/eeap.php?rel=b
The rule for index works fine with or without a trailing slash. However when I hit http://mydomain.com/testsite/about/ the page is served without CSS and images and works fine if the last slash is removed. This happens with the rule for ict too.
For the next rule (eeap) things behave the other way. CSS and images go missing if I end the URL with a slash.
What am I doing wrong here?
You need to think about this from the point of view of the web browser.
If the URL does not end with a slash, "/about" for example, then the web browser assumes that it is looking at a file called "about" in the top-level directory of the web server. So any requests in the page generated by "/about" which have relative paths will cause the web browser to produce a full path based on starting off in the top-level directory. For example, a CSS file path of "css/style.css" will cause the browser to start from the top-level and ask for the file from "/css/style.css".
But if the URL does end with a slash, "/about/" in our example, then the web browser believes that it is looking at a sub-directory called "about". Now that same CSS file path "css/style.css" will cause the web browser to ask for the file from "/about/css/style.css".
One CSS file path will be correct, the other will not. Hence the behaviour you're seeing.
There are a few ways to avoid this. You can tell Apache to permanently redirect all requests to either the URL which ends with a slash, or the URL which does not, so that all visitors will end up looking at the same URL. This makes it possible to know what relative CSS and image paths to use in your web pages.
Or you can modify your web pages so that all file paths for CSS, image, JavaScript, etc, are anchored to the top-level directory. So "css/style.css" would become "/css/style.css" for example (note the forward-slash at the start of the path to tell the browser to start from the top-level directory).
Or you could add a bunch of convoluted mod_rewrite rules to try to catch all possible outcomes for CSS files and the like. I don't favour this approach because it will usually make your ruleset more fragile and complicated.
i'm quite new in the world of Url Rewriting. I have to do so into our own CMS to have a good looking url and to get a better structure that gives results with search engine (SEO).
This is the background file that do all the job that we now hide with nice URL's:
http://something.com/fr/index.php
This is an example of my structure pattern :
http://something.com/fr/accueil or ...
http://something.com/fr/produits/solives-de-rive-rimboard
The CMS simply create an HTACCESS file into the FR directory for quite basic rewriterule like the following :
Options +FollowSymlinks
RewriteEngine On
RewriteRule produits/solives-de-rive-rimboard index.php?p=144
RewriteRule produits/decoupe-sur-mesure index.php?p=145
RewriteRule produits/panneaux-osb index.php?p=146
RewriteRule produits/boites-de-bois-crates index.php?p=147
RewriteRule produits/palettes-de-bois index.php?p=148
RewriteRule accueil index.php?p=4
RewriteRule a-propos-de-cimdat index.php?p=139
RewriteRule produits index.php?p=140
RewriteRule videos index.php?p=141
RewriteRule nous-joindre index.php?p=142
All works fine for first level page like "accueil", "nous-joindre"...
but I haven't found the work around for page of second and third level like "produits/solives-de-rive-rimboard". The index.php just load fine except that all relative link to this page are a subfolder away (the css, the image, jquery...).
Is there any tag that I can use into the htaccess file to specify that no matter in which level "accueil", "produits/something", or a third level one like "produits/group/something" for the index.php have a basic path ?
I'll prefer looking a work around within the htaccess instead of giving index.php a "<base href="something">" that might give us other problems in our structure.
Thanks
This is an HTML issue, rather than a server or .htaccess problem. If your browser sees something like /category/product it is going to act as if it is in the /category/ folder regardless of where the internal server side redirects are sending the request.
The fix to this is simple, change all of the linking in your html to be relative to the site root. So if you have an image tag like
<img src="img/button.gif" />
change it to
<img src="/img/button.gif" />
This tells the browser the exact path from the root to request files from regardless of your rewrite rules. This needs to be done for all of your relative links, including css and javascripts. It may be a bit of work to do, but once it is done it should make future maintenance much easier since you won't have to worry about the apparent path to the page.
Yes, this could be done via .htaccess fairly simply, but there are side effects, including the fact that search engines could see it as duplicate content and it could increase your server bandwidth use as users can't cache static content efficiently. Your best bet is to follow the best practice now while the site is new and growing than try to fix it later after a workaround has caused problems.