Can I write an HTML response from an .htaccess file - apache

Apologies if this is a stupid question, but can I control the HTML response from a .htaccess file (Apache)?
In other words something like (psuedo code) Write <!DOCTYPE html><html>...[etc]
The reason I ask is because I would like to "take down" some sites in one "hit", but without replacing any files or having any other kind of holding page.

I found the answer myself, certainly worked for what I needed:
ErrorDocument 503 "<!DOCTYPE html><html><head><title>This website is undergoing maintenance</title></head><body style='font-family: sans-serif'><h1>This website is undergoing maintenance</h1></body></html>"
RewriteEngine On
RewriteRule .* - [R=503,L]
Hope this helps somebody

No its not possible to produce HTML content into a rewritten URI. However what you can do is to have a HTML file pre-written let's call it outage.html which will be placed in your DOCUMENT_ROOT.
Then enable mod_rewrite and .htaccess through httpd.conf and place this code on top of your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
#RewriteRule (?!^outage\.html$)^.*$ /outage.html [L,NC]
Whenever you want to bring the site down just uncomment above RewriteRule line by removing # and your site will just show outage.html to visitors for every URL.

Related

.htaccess mod_rewrite.c using HTTP_REFERER

I am trying to get .htaccess to fetch the page http://www.example.org/aa/exists.php when http://www.example.org/aa/doesntexist.php is entered in the URL bar. The .htaccess file is clearly functional, because the DirectoryIndex line is producing the desired result, with http://www.example.php in the URL bar fetching the page http://www.example.php/aa/default.php.
I tried adapting the response to how to redirect using HTTP_REFERER on htaccess to my situation, but without success.
Below is the full text of my .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} ^(.*)www\.example\.org/aa/doesntexist\.php [NC]
RewriteRule ^(.*)(www\.example\.org/aa/)doesntexist\.php(.*)$ $1$2exists.php$3 [NC,L]
DirectoryIndex aa/default.php
</IfModule>
A request for http://www.example.org/aa/doesntexist.php yields the following error:
Not Found
The requested URL /aa/doesntexist.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Try adding Options +FollowSymlinks and Allow Override All as first commands before turning the engine on.
I couldn't see anything wrong in your condition or rule. I recently had a very similar problem that could be solved that way.
If that's not the problem, you might have a look at http://forum.modrewrite.de/topic82.html
It's in German, unfortunately, but maybe stil helpful - at least by aid of google translate (surely not perfect, but good enough).
Btw.: if there is a reason for checking the referrer and not only the requested URI, keep it. Else, the rule alone will do what you want. There is no need for a condition if the content of the condition matches the pattern of the rule.

Seo-Friendly url's with .htaccess

I already found several topics and questions regarding this, but it seems everything I'm trying to do doesn't do what it is supposed to do:
.htaccess-file
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?site=$1
As you can tell, nothing to special. This example was taken from this solution here: SEO Friendly URL (with .htaccess)
From my example:
http://example.org/index.php?site=delivery
should result in
http://example.org/Delivery # If it is possible, a capitalized Letter would be cool
I can call the Site manually, but references (to *.css-files) aren't included anymore. Furthermore, the RewriteBase doesn't seem to work.
I added some buggy code to verify that the .htaccess file is loaded (and it is).
If I linke something with <a href="index.php?site=mySite"> - does the URL automagically change to the specific rule? Yes, right? But what could be missing, that the rules aren't applying?
Thank you in advance
Your rule is fine. Because of the rewrite, you have to handle relative links differently now.
So once the rule is in place. You need to add this to the <head> section of your html.
<base href="http://www.yoursite.com" />
Then your JS and CSS will load while using the rule. It's not a .htaccess rule problem.
If I linke something with - does the
URL automagically change to the specific rule?
No it does not. You need a rule for that too.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond {THE_REQUEST} ^[A-Z]{3,}\ /+index\.php\?site=(.+)
RewriteRule ^ %1? [R=301,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?site=$1

My rewrite rule isn't working

I currently want my site to rewrite from
.com/page/1234
to
.com/?view=page&id=1234
Heres my .htaccess content:
RewriteBase /
RewriteRule ^/page/([0-9]+)$ /index.php?view=page&id=$1
ErrorDocument 404 errors/404.html
When I type ".com/page/1" my site just redirects to the 404 page.
What have I done wrong? I tried to the simplest:
RewriteRule ^/page$ /about.php
But it doesn't work either. So I'm having some suspect that 000webhost (my current host) is not supporting RewriteRule although they stated they support it.
From personal experience, I know they -do- support RewriteRule, but it is somewhat horrible to test them. In "per-directory"-context, the slash from a directory is appended to the "prefix" part of the url. .htaccess always works as in "per-directory"-context. A RewriteRule that begins with a slash in .htaccess will therefore never match anything.
If you change your .htaccess to the following, everything should work as expected:
RewriteBase /
RewriteRule ^page/([0-9]+)$ /index.php?view=page&id=$1
ErrorDocument 404 errors/404.html
I recommend reading the documentation for mod_rewrite. It contains a lot of useful information.

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

Htaccess redirect or rewrite

I'm in the process of testing a site at a preview URL until the DNS transfers over. I'm previewing the site at http://IP_ADDRESS/~name/.
All of my images that have been uploaded in the CMS are listed like <img src="/uploads/images/">.
But this doesn't work while i'm on the preview URL. So ideally I need to redirect /uploads/* to /~name/uploads/*
I was wondering if this should be a redirect or a rewrite in htaccess and what the rule might be?
Thanks.
This is the code you'll need in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^uploads/ ~name%{REQUEST_URI} [L,NC]